Minimizing PowerShell Windows while executing

Okay, I realize it has been a really long time since I last posted anything… Essentially my role changed, but I am back.

I recently came across an issue whereby I have been executing PowerShell scripts to perform various functions on some digital signage and the PowerShell window was clearly visible. i.e. in these cases the last thing you want to see is a window open running a PowerShell script.

After much back and forth I realized that the recommended method (or so I was told) was to launch PowerShell minimized. For some reason I could not get this to work in every scenario, so the next best thing was to look for a running instance of PowerShell and minimize the window. Now to caveat my solution, I only tend to have one instance of PowerShell running at any one time, so here is what I came up with:


'[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'



= @(Get-Process

If ($PSId
$NULL) { [Win32.NativeMethods]::ShowWindowAsync($PSId,


Note that the IF statement was to handle an exception when I was executing from the PowerShell editor i.e. if there is no instance of PowerShell running, the script would throw an error when trying to set the selected PowerShell instance to mimimized..

To use this, just copy the lines above early in your PowerShell script and while you will briefly see the PowerShell window open, it will minimize itself quickly.