Last active
May 7, 2024 13:46
-
-
Save Nora-Ballard/11240204 to your computer and use it in GitHub Desktop.
Hide, Show, Minimize, Maximize, etc window from Powershell.
This file contains 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
hey Nora - really great work here!
I ran into a few issues getting the gist to run on my Win 11 setup. To make it easier than finding minor changes in this thread, I posted a new repo here for your consideration. https://github.com/surfaceowl/Set-WindowState.ps1
There are three files there to help see the changes in steps-
Using Set-WindowState-v2-basic-fixes.ps1 as an example, once this function is referenced in your powershell profile you can run from your terminal anywhere with something like: Get-Process *Evernote* | Set-WindowState -State "MINIMIZE" ...that just finds all the instances of a process and then minimizes
This is super helpful for managing annoying recurring tasks in Task Scheduler you can put this in a script file (e.g. paste cmd above into a file named
minimize_evernote.ps1
and setup a new task in Task Scheduler. The cmd below will run the script silently, importantly avoiding a quick pop-up or flash of a powershell window that some may find annoying or worrisome (especially if run at startup). Configure the task with:Program/script: cmd
Add arguments:
(of course, change the line above to match whatever path and filename used on your system)