Today I was asked a question… how to hide a PowerShell window.
In this specific scenario a script was executed by calling the PowerShell.exe executable with some appropriate parameters:
1 |
PowerShell.exe -WindowStyle Hidden -File D:\MyScript.ps1 |
But… what if you have no such luxury? What if you only have a script and no control over how it’s called, yet you do want to hide the screen so that the user doesn’t see it?
Put the following code at the top of your script:
1 2 |
Add-Type -Name win -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);' -Namespace native [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle,0) |
Well, it’s not hiding entirely (one sees a prompt for an instance) but it’s pretty darn close 🙂