Skip to content

Instantly share code, notes, and snippets.

@Nora-Ballard
Last active May 7, 2024 13:46
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save Nora-Ballard/11240204 to your computer and use it in GitHub Desktop.
Save Nora-Ballard/11240204 to your computer and use it in GitHub Desktop.
Hide, Show, Minimize, Maximize, etc window from Powershell.
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'
@surfaceowl
Copy link

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-

  1. Set-WindowState.ps1 -- directly from Nora's gist, for reference / diffs
  2. Set-WindowState-v2-basic-fixes.ps1 -- corrects quote and dash chars that cause powershell errors on my install
  3. Set-WindowState-v3-style-suggestions.ps1 -- building on v2, some minor order/style changes

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:

/c start /min "" Powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Users\chris\dev\powershell_scripts\minimize_evernote.ps1"

(of course, change the line above to match whatever path and filename used on your system)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment