Last active
March 18, 2025 15:24
-
-
Save Nora-Ballard/11240204 to your computer and use it in GitHub Desktop.
Hide, Show, Minimize, Maximize, etc window from Powershell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-WindowState { | |
param( | |
[Parameter()] | |
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', | |
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', | |
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')] | |
[Alias('Style')] | |
[String] $State = 'SHOW', | |
[Parameter(ValueFromPipelineByPropertyname='True')] | |
[System.IntPtr] $MainWindowHandle = (Get-Process –id $pid).MainWindowHandle, | |
[Parameter()] | |
[switch] $PassThru | |
) | |
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); | |
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru | |
} | |
PROCESS | |
{ | |
$Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$State]) | Out-Null | |
Write-Verbose ("Set Window State on '{0}' to '{1}' " -f $MainWindowHandle, $State) | |
if ($PassThru) | |
{ | |
Write-Output $MainWindowHandle | |
} | |
} | |
END | |
{ | |
} | |
} | |
Set-Alias -Name 'Set-WindowStyle' -Value 'Set-WindowState' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can check my fork. I store the
MainWindowHandle
s in a.json
file in"$env:APPDATA\WindowHandles.json"
and restore them every time the script is run. These entries are cleared after a threshold (default = 24h).