2

I'm calling Powershell like so:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noninteractive -nologo -file "C:\Users\dummy\Documents\dev\powershell\samples\test.ps1"

I'm calling it from a python script, but the same problem can be observed if called via a shortcut. I thought the -NonInteractive flag would cause Poweshell to execute in a hidden window, but it doesn't. Is there a way of supressing the console window when calling Powershell from an external application?

Solution based on Johannes Rössel suggestion

import subprocess
st_inf = subprocess.STARTUPINFO()
st_inf.dwFlags = st_inf.dwFlags | subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(["notepad"], startupinfo=st_inf)
Community
  • 1
  • 1
guillermooo
  • 7,915
  • 15
  • 55
  • 58

3 Answers3

2

You can pass appropriate arguments to CreateProcess or Process.Start to suppress the console window.

However, PowerShell also has a -WindowStyle parameter which you can set to hidden.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

I had no luck with -WindowStyle Hidden, because a console window appeared every time for a while.

That's why I use a helper exe called PsRun.exe that does exactly that. You can download source and exe file Run scheduled tasks with WinForm GUI in PowerShell. I use it for scheduled tasks.

(Note that -windowstyle parameter is available only for V2.)

stej
  • 28,745
  • 11
  • 71
  • 104
0

Use -WindowStyle Hidden. See here.

CesarGon
  • 15,099
  • 6
  • 57
  • 85