1

I'm trying to restore an application window from a minimized state, except in my case it's an Oracle VirtualBox VM window running alongside multiple VMs. They all have the same process name which means I can't restore by process name which would be easy. I need to restore by mainWindowtitle which is the unique property. Though I have a PowerShell script I've been tinkering with, it's just exiting with no error message. While no error message is logged when the script runs, something just appears to flash for a millisecond, but then nothing happens afterwards - all windows remain minimized. The window title I want to restore contains DC2. The full title of the window is: DC2 [Running] - Oracle VM VirtualBox. Neither using the full window title name nor a partial name work. The PowerShell script:

Clear-Host
Function Set-WindowStyle 
{
    param
    (
        [Parameter()]
        [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 
            'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 
            'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
        $Style = 'SHOW',
        [Parameter()]
        $MainWindowHandle = (Get-Process -Id $pid).MainWindowHandle
    )

    $WindowStates = @{
        FORCEMINIMIZE = 11; HIDE = 0
        MAXIMIZE = 3; MINIMIZE = 6
        RESTORE = 9; SHOW = 5
        SHOWDEFAULT = 10; SHOWMAXIMIZED = 3
        SHOWMINIMIZED = 2; SHOWMINNOACTIVE = 7
        SHOWNA = 8; SHOWNOACTIVATE = 4
        SHOWNORMAL = 1
    }
    Write-Verbose ("Set Window Style {1} on handle {0}" -f $MainWindowHandle, $($WindowStates[$style]))

    $Win32ShowWindowAsync = Add-Type –memberDefinition @” 
    [DllImport("user32.dll")] 
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

    $Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$Style]) | Out-Null
}

# Usage

# Minimize a running process window
#Get-Process -Name Taskmgr | %{Set-WindowStyle MINIMIZE $PSItem.MainWindowHandle}
#Get-Process -Name DC2* | %{Set-WindowStyle MINIMIZE $PSItem.MainWindowHandle}

# Restore a running process window - the last window called will be topmost
#Get-Process -Name DC2 | %{Set-WindowStyle RESTORE $PSItem.MainWindowHandle}
#Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize
Get-Process | Where-Object {$_.MainWindowTitle -Contains "DC2"} | %{Set-WindowStyle RESTORE $PSItem.MainWindowHandle}

Here are all my window titles:

PS C:\Users\Admin1> Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize

   Id Name           MainWindowTitle                                                                                                       
   -- ----           ---------------                                                                                                       
 9668 powershell_ise Windows PowerShell ISE                                                                                                
 5152 ServerManager  Server Manager                                                                                                        
 2504 VirtualBoxVM   CentOS2 [Running] - Oracle VM VirtualBox                                                                              
 4468 VirtualBoxVM   WIN10 [Running] - Oracle VM VirtualBox                                                                                                                                                            
 8812 VirtualBoxVM   DC2 [Running] - Oracle VM VirtualBox                                                                                  
 9028 VirtualBoxVM   MGMT1 [Running] - Oracle VM VirtualBox                                                                                
10252 VirtualBoxVM   CentOS1 [Running] - Oracle VM VirtualBox                                                                              
10752 VirtualBoxVM   ADFS1 [Running] - Oracle VM VirtualBox                  

                                                   
T-Heron
  • 5,385
  • 7
  • 26
  • 52
  • Thank you, I did find that on my own about 10 minutes before you posted your comment. I guess our minds think alike. Happy holidays to you! – T-Heron Nov 26 '22 at 17:36

1 Answers1

0

[This answer][1] did the trick:

Add-Type -AssemblyName UIAutomationClient

$MyProcess = Get-Process | where { $_.MainWindowTitle -like "DC2*" }
if ($null -ne $MyProcess) { 
    # if Minimized make it Normal
    $ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
    $wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
    if ($wp.Current.WindowVisualState -eq 'Minimized') {
        $wp.SetWindowVisualState('Normal') 
    }
    # execute needed function
}
else {
    start-process "C:\Program Files (x86)\Test\ccTest.exe" -WindowStyle Normal
    # execute needed function
}


  [1]: https://stackoverflow.com/questions/70629307/with-powershell-how-to-switch-minimized-application-to-normal-state?rq=1
T-Heron
  • 5,385
  • 7
  • 26
  • 52