1

This was the piece of code I am referring to https://stackoverflow.com/a/73587858/10813659 in my previous StackOverflow post.

Running my Python code within the terminal does not keep opening and closing the window but it does when creating an exe with auto-py-exe with 'hide console window'. I was wondering if there is a way to suppress this behavior as it is very distracting when running my simple command while working.

Right now, keeping the application 'console based' suppresses this issue but as soon as I want to hide the console and go 'Window Based', the PowerShell command keeps opening and closing.

The piece of code in question:

import subprocess

p = subprocess.run(
     ["powershell.exe", 
      "-NoProfile", 
      "-ExecutionPolicy", "Bypass", 
      "-File", "C:/Users/xxxxx/Documents/betterNetstatOut.ps1"], 
     capture_output=True, text=True
    )
Zarif Rahman
  • 79
  • 1
  • 8
  • 1
    I didn't consider that `-WindowStyle Hidden` cannot prevent the flashing of the window: a console window is invariably shown first and _then_ it is hidden. The linked duplicate discusses various techniques for launching the console window invisibly to begin with. – mklement0 Sep 15 '22 at 20:48
  • 1
    A VBScript solution is in [this answer](https://stackoverflow.com/a/41229169/45375). – mklement0 Sep 15 '22 at 20:55
  • A [potential future system-level enhancement](https://github.com/microsoft/terminal/blob/dev/duhowett/spec/console-allocation/doc/specs/%237335%20-%20Console%20Allocation%20Policy.md) is being discussed: _conditional_ allocation of a console, depending on runtime conditions. Once that becomes available, the PowerShell CLI would have to opt-in to offer this feature, but note that only the PowerShell (Core) 7+ CLI, `pwsh.exe`, would ever receive such an update, not the Windows PowerShell CLI, `powershell.exe`, given that Windows PowerShell is in maintenance-only mode. – mklement0 Sep 15 '22 at 21:21
  • 1
    This is exactly what I'm looking for! Sadly this hasn't been updated since 2020. – Zarif Rahman Sep 15 '22 at 22:15
  • 1
    Another issue that I am not finding immediate answers to is how I can capture data from my VBScript that is running my PowerShell commands. I can obviously save the data to a txt and read it back but I do wonder if there is a better way to handle this. – Zarif Rahman Sep 15 '22 at 22:52
  • 1
    I was able to fix this issue, funnily enough, the VBScript method ended up not working but this: https://stackoverflow.com/a/7006424/10813659 worked smoothly, made two line changes, and the command line never propped up. – Zarif Rahman Sep 17 '22 at 01:50
  • Nice find. I've added it as the first duplicate. – mklement0 Sep 17 '22 at 02:11

0 Answers0