0

I've got a program that I converted to an exe using auto-py-to-exe and pyinstaller. The problem is that some commands require powershell, and whenever I use powershell it pops up despite me hiding the console. I run the command like this:

command = [POWERSHELL_PATH, '-ExecutionPolicy', 'Unrestricted', 'echo test']
            process_result = subprocess.run(toggle_command_1, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                            universal_newlines=True)

When running the code in my ide (Pycharm) powershell does not popup, but as an exe it does. Any ideas on what I can do?

Edit 1:

I've tried the soultions in How to run a PowerShell script without displaying a window? but it didn't work. Thanks for the suggestion though :)

Edit 2:

When running the exe the cmd console does not appear, but when a powershell process occurs powershell pops up (with no dialouge) and then disappears when the process is complete. All powershell processes are single line commands as in the example above. I really need a fix for this so please help. Thanks :)

Edit 3:

I have tried --noconsole and --windowed. These do hide the original cmd window (which I also need), but the powershell processes still popup.

Thanks

john
  • 11
  • 4
  • 1
    Does this answer your question? [How to run a PowerShell script without displaying a window?](https://stackoverflow.com/questions/1802127/how-to-run-a-powershell-script-without-displaying-a-window) – matszwecja Sep 19 '22 at 10:25

2 Answers2

0

you can try this Ref --assuming you using Pyinstaller to convert .py to .exe

python pyinstaller.py --noconsole johnScript.py
  • This works for the cmd window that appears when you run the exe, I have already done this. :) – john Sep 28 '22 at 14:45
0

After alot of searching, I found a solution for another way of running commands, and tried it on my code. Intially:

command = [POWERSHELL_PATH, '-ExecutionPolicy', 'Unrestricted', 'echo test']
            process_result = subprocess.run(toggle_command_1, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                            universal_newlines=True)

By simply adding shell="False":

command = [POWERSHELL_PATH, '-ExecutionPolicy', 'Unrestricted', 'echo test']
            process_result = subprocess.run(toggle_command_1, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                            universal_newlines=True, shell="False")

This stops the powershell window from showing

john
  • 11
  • 4