Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'
@Fazered

This comment has been minimized.

Copy link

@Fazered Fazered commented Sep 25, 2014

Rename the $State parameter to $Style to get this to work.

@Paluan

This comment has been minimized.

Copy link

@Paluan Paluan commented Dec 16, 2014

thanks for this amazing script, just one thing to add
" “@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru"
my powershell ask to remove spaces at beginig of this line

@flamingtoast13

This comment has been minimized.

Copy link

@flamingtoast13 flamingtoast13 commented Jun 10, 2015

Perhaps this is outdated, but anyway, this script works really well and it would be cool if it accepted pipeline input

@lalibi

This comment has been minimized.

Copy link

@lalibi lalibi commented Oct 27, 2015

Check my fork, I added pipeline support

@ed1chandler

This comment has been minimized.

Copy link

@ed1chandler ed1chandler commented Oct 28, 2016

Very useful, but isn't this really setting the window's "state", not its "style"?
State - https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
Style - https://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
Any ideas on how the actual "style" can be set?
Thanks.

@nspb001

This comment has been minimized.

Copy link

@nspb001 nspb001 commented Jan 8, 2018

Any idea how to minimize/hide a window that doesn't have a MainWindowHandle property? For example, starting printui.exe you will receive a MainWindowHandle=0. The window doesn't have buttons to minimize/maximize either. However, using win+d to minimize the desktop, minimizes the printui.exe process as well.

@Nora-Ballard

This comment has been minimized.

Copy link
Owner Author

@Nora-Ballard Nora-Ballard commented Apr 16, 2018

Thank you everyone for the feedback. I have made some updates to address everyone's feedback. I hope you find it useful.

@ed1chandler is correct that state is probably more accurate, I wrote this originally for hiding the PS console and named it off of the parameter on there (-WindowsStyle) which sets these properties. I have updated it to reflect that, and added aliases for 'style' to keep compatibility, but you can easily disable those if you need.

I fixed the bug @Paluan reported, that was my typo when I was trying to clean up the formatting.

I have updated the MainWindowHandle parameter to accept pipeline input by property name, so it will accept a process object piped into it now.

I have update the script to output the window handle object if the -PassThru parameter is provided.

@nspb001 I'm not sure. Perhaps starting it with Start-Process or System.Diagnostics.Process would have it on that process object, or perhaps it is a permissions issue with it running elevated?

@dazlab

This comment has been minimized.

Copy link

@dazlab dazlab commented Oct 20, 2020

Is there any way to get this to open up in fullscreen?

@Nora-Ballard

This comment has been minimized.

Copy link
Owner Author

@Nora-Ballard Nora-Ballard commented Nov 25, 2020

Is there any way to get this to open up in fullscreen?

I don’t think so, at least not with the API call that is being used currently. We are just calling an existing Windows API function and giving it values that it accepts.

@Offertorium

This comment has been minimized.

Copy link

@Offertorium Offertorium commented Nov 19, 2021

Hi, what is the best way to minimize a window if the process name has a period. The process looks like this name_1.0 But the script runs very well otherwise. Thank you very much for that

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