Skip to content

Instantly share code, notes, and snippets.

@JamesDBartlett3
Forked from lalibi/Set-WindowState.ps1
Last active November 30, 2022 20:35
Show Gist options
  • Save JamesDBartlett3/4436ba68ed85273185c98a646fc5583f to your computer and use it in GitHub Desktop.
Save JamesDBartlett3/4436ba68ed85273185c98a646fc5583f to your computer and use it in GitHub Desktop.
Hide, Show, Minimize, Maximize, etc. any application window from PowerShell.
# WARNING: NON-WORKING CODE - DO NOT USE IN PRODUCTION!
#TODO: Implement $AppName & $MainWindowHandle parameters
function Set-WindowState {
<#
.SYNOPSIS
Set a given window state using WinAPI.
.DESCRIPTION
Use the ShowWindowAsync function to set the Window state for
any given window handle or the current powershell process.
.EXAMPLE
Set-WindowState -State "MINIMIZE"
Minimizes the window where the command is executed.
.EXAMPLE
Set-WindowState -State "MINIMIZE" -MainWindowHandle (ps chrome | select -First 1).Handle
Minimizes the window of the given handle.
.LINK
https://gist.github.com/Nora-Ballard/11240204
#>
[CmdletBinding(DefaultParameterSetName = 'InputObject')]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[Object[]] $InputObject,
[Parameter(Position = 1)]
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
[string] $State = 'SHOW',
[string] $AppName = $null
[System.IntPtr] $MainWindowHandle = ((Get-Process $AppName).MainWindowHandle | Sort -Desc | Select -First 1)
[switch] $SuppressErrors = $false,
[switch] $SetForegroundWindow = $false
)
Begin {
$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 "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 ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment