Powershell appears after converting program to exe

Question:

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

Asked By: john

||

Answers:

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

python pyinstaller.py --noconsole johnScript.py
Answered By: Bhargav

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

Answered By: john