Tuesday, December 23, 2008

v2 CTP3: Running PowerShell silently

Well, if you haven't heard, v2 CTP3 is out. Check it HERE.

This new release provides a feature where powershell.exe can be run with a hidden window/screen. This is definitely an FAQ candidate: I've seen this asked many times in various forums.

PS>powershell.exe /?
Sets the window style to Normal, Minimized, Maximized or Hidden.

(The CTP3 release notes have a typo: This parameter is covered as "-WindowsStyle".)

So if you run something simple like:
PS>powershell.exe -windowstyle hidden # from PowerShell

Be prepared... Not only will your current PowerShell console vanish, but you'll also have another PowerShell host running.

What do I mean, if you try the above, then open another PowerShell console, you could see:
PS> gps powershell

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
331 6 21396 7640 127 0.94 904 powershell
168 6 24384 33332 127 0.61 4752 powershell
210 6 25816 33936 129 0.53 5324 powershell

So, running with "hidden" hides the current process you just invoked, but also hides the console from which it was called/invoked in the first place.

This new hidden features is definitely useful for stuff like scheduled tasks where the solution before was to either wrap the PowerShell in VBScript or even use the Win32 APIs to do something more complicated.