Functions/Set-WindowState.ps1
function Set-WindowState { <# .SYNOPSIS Sets the window state of a currently running application .DESCRIPTION Sets the window state of a currently running application .PARAMETER InputObject Describe parameter -InputObject. .PARAMETER State Which state you want to place the application into. Validate set: 'FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL' Defaults to 'SHOW' .PARAMETER SuppressErrors Control error processing .PARAMETER SetForegroundWindow Determines whether the application will become the foreground (currently selected) application .EXAMPLE Get-Process notepad | Set-WindowState -State MAXIMIZE -SetForegroundWindow Will take the currently running Notepad.exe process, maximize it, and put it into the foreground .NOTES Most of this code was taken from GitHub. I added help, put in Write-Verbose, option for pushing the application to the foreground .LINK https://gist.github.com/Nora-Ballard/11240204 #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] [CmdletBinding(DefaultParameterSetName = 'InputObject')] param( [Parameter(Position = 0, Mandatory, ValueFromPipeline)] [Object[]] $InputObject, [Parameter(Position = 1)] [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')] [string] $State = 'SHOW', [switch] $SuppressErrors = $false, [switch] $SetForegroundWindow = $false ) begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" $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 } $Win32ShowWindowAsync = Add-Type -MemberDefinition @' [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd); '@ -Name 'Win32ShowWindowAsync' -Namespace Win32Functions -PassThru if (!$global:MainWindowHandles) { $global:MainWindowHandles = @{ } } } process { foreach ($process in $InputObject) { $handle = $process.MainWindowHandle if ($handle -eq 0 -and $global:MainWindowHandles.ContainsKey($process.Id)) { $handle = $global:MainWindowHandles[$process.Id] } if ($handle -eq 0) { if (-not $SuppressErrors) { Write-Error -Message "Main Window handle is '0'" } continue } $global:MainWindowHandles[$process.Id] = $handle $Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null if ($SetForegroundWindow) { $Win32ShowWindowAsync::SetForegroundWindow($handle) | Out-Null } Write-Verbose -Message ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State) } } end { Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |