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.

  • I actually dont want to run powershell script but command – hath hassan Jun 14 at 23:52
  • if you see above I tride to run the same command you gve it to me – hath hassan Jun 14 at 23:53
  • What happens if you first run: CD /D "C:\Windows\System32\WindowsPowerShell\v1.0" and then after that run powershell -windowstyle hidden -command get-childitem -path c:\. I was thinking you were experiencing a simple issue like that as such or perhaps a syntax issue or something as I did just as I wrote the examples below and it worked just fine from Windows 10. What version of PowerShell are you running exactly when you run $PSVersionTable what version are you running? – Pimp Juice IT Jun 15 at 15:52

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

  • 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. – Pimp Juice IT Jun 14 at 2:23

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})
  • but the language of the code is c# or powershell – hath hassan Jun 14 at 22:35
  • can you give me a method for vb.net – hath hassan Jun 14 at 22:36

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.