10

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.

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
hath hassan
  • 101
  • 1
  • 1
  • 3

2 Answers2

7

Run a PowerShell Command Silently from a Prompt

As stated. . .

"You can use PowerShell.exe to start a PowerShell session from the command line of another tool, such as Cmd.exe, or use it at the PowerShell command line to start a new session. Use the parameters to customize the session."


-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>

Verifiable Examples

1. Command Prompt (cmd)

powershell -windowstyle hidden -command get-childitem -path c:\ ^| out-file "C:\Folder\Log\log.txt"

enter image description here

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:\Folder\Log\log.txt"

enter image description here

Note: After running, open log.txt to verify its content since out-file directs it the output.


Further Resources

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
  • If you get an error stating something along the lines of "***Powershell' is not recognized~***" you can firstly and simply run `CD /D "C:\Windows\System32\WindowsPowerShell\v1.0"` and then run the PowerShell commands with applicable parameters and such directly afterwards until you get your environmental variables straightened out otherwise. – Vomit IT - Chunky Mess Style Jun 14 '18 at 02:23
  • Is it possible to write something in the PowerShell script so it will hide the windows when it starts? – JinSnow Feb 15 '21 at 16:03
  • 1
    I thought the #2 example did that. I'd have to dig and test to confirm but I know I've done this before with logon scripts with a group policy so I know it's possible. Test a variation of the #2 example logic and see what you get. If it does not work as you need it, open a question with what you are trying and someone will help you get it figured out. – Vomit IT - Chunky Mess Style Feb 15 '21 at 16:30
  • Thanks but, for information, `powershell -windowstyle hidden` on the top of my PowerShell script did not work (I launch it by clicking on its "as admin" shortcut). – JinSnow Feb 21 '21 at 15:23
  • 1
    I see, see if you change it up a bit and allow the PowerShell execution to do the "run as admin" by using the **PowerShell (Solution 1)** as written about here https://superuser.com/questions/1619630/run-powershell-script-as-a-different-user-and-elevated/1619700#1619700 and see if executing the PS this way with elevated admin and not use the shortcut to do that. Does this solution the problem you mention here by chance? @JinSnow The `Start-Process` line in particular. – Vomit IT - Chunky Mess Style Feb 21 '21 at 18:04
  • 1
    Even though windowstyle works somewhat, the powershell window is still shown for a split second. – Tigerware Jun 29 '21 at 18:12
  • 1
    Thanks @BluE I'll see if I notice this when I test with it. I know you can run as `SYSTEM` via task scheduler to keep it hidden and just execute PS, or with a login script with GP option to not show running login scripts (or something like that) but it's been a while since I've messed with it. – Vomit IT - Chunky Mess Style Jun 29 '21 at 18:45
  • Well, I had it running from task scheduler, so that might have caused it too. The key was to let it run independently of logon. Now I don't notice the task anymore. Depending on the use case, this setting could cause other problems, though. – Tigerware Jun 29 '21 at 18:54
  • @BluE True, it's all about under what security context, I totally agree! I like machine level startup scripts for machine level stuff as those run as `SYSTEM` by default and just work. Keep it chugging like a train.... choo, choo!!! All aboard!! – Vomit IT - Chunky Mess Style Jun 29 '21 at 21:10
1

Not sure where I picked these lines up at but some nice functions to show and hide the console.

Show/Hide Powershell Window

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:

Map: Show/Hide Powershell Windows Checkbox

$cb_PSCheckbox.Add_Checked({Show-Console})
$cb_PSCheckbox.Add_UnChecked({Hide-Console})