I want to execute a PowerShell command silently with no blue screen.
How can I do this from the PowerShell command?
I tried this. . .
PowerShell.exe -windowstyle hidden
but it didn't workâthe command was executed but with the blue screen still.
As stated. . .
-WindowStyle
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
-Command
Executes the specified commands (and any parameters) as though they were typed at the PowerShell command prompt, and then exits, unless the NoExit parameter is specified. Essentially, any text after
-Command
is sent as a single command line to PowerShell
Syntax
powershell -windowstyle hidden -command <PowerShell Command String>
1. Command Prompt (cmd)
powershell -windowstyle hidden -command get-childitem -path c: ^| out-file "C:FolderLoglog.txt"
Note: With cmd the [
|
] pipe symbol needs escaped with the [^
] caret symbol so "^|
".
2. PowerShell Prompt
powershell -windowstyle hidden -command get-childitem -path c: | out-file "C:FolderLoglog.txt"
Note: After running, open log.txt to verify its content since
out-file
directs it the output.
Not sure where I picked these lines up at but some nice functions to show and hide the console.
Function Show-Console {
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 5)
}
Function Hide-Console {
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
}
I use it for my gui apps when I want to hide the ps in background:
$cb_PSCheckbox.Add_Checked({Show-Console})
$cb_PSCheckbox.Add_UnChecked({Hide-Console})