'How to run python code from powershell with no command prompt showing?
I would like to be able to run a python script from a .ps1 file without the terminal window showing up to run the script. I would think that there is some extra command I can put at the end to make it run windowless, but I am not sure what it is.
The only code I have in the ps1 file is to execute the python script by linking the path.
I do not want to run the code some way else, it has to be from a ps1 script since I will be adding to it eventually to have more features. I also would rather not have to change the file to a .pyw if possible.
E:/script.py
*Note - I ran the file from a different drive this time, but I want it to be from E:/ like I have in my code.
What I don't want to show up:

Solution 1:[1]
In a comment you state:
im using run (WIN + R) and this command:
powershell -w h iex (E:\a.ps1)
-w his short for-WindowStyle Hiddeniexis short forInvoke-Expression, which is not needed here - and should generally be avoided: see this answer.
Note: While this does make the console window that the PowerShell session itself runs in invisible, the window becomes invisible only after briefly showing first.
To avoid this brief flashing of the console window,
powershell.exemust be launched indirectly, namely via a GUI-subsystem application that launches it with a hidden window; the bottom section of this answer discusses options.Also note that supporting this directly in the future is planned, but will likely only be implemented for the CLI of PowerShell (Core) 7+,
pwsh.exe- see GitHub issue #3028.
With that out of the way:
Console applications (including
.ps1scripts) invoked directly from a hidden PowerShell session run hidden too, because they run directly in the hidden console window.By contrast, any GUI applications or applications of either type invoked via
Start-Processrun visibly.- While you can generally use
Start-Process -WindowStyle Hiddento run GUI applications invisibly as well, this, unfortunately, does not seem to work with.pywfiles, i.e.pythonw.exe-launched scripts, which still run visibly.[1]
- While you can generally use
By default, .py files invoked directly execute via py.exe, which is a console application.
Strangely, your screen shot suggests that it is not in your case.
py.exe is normally just a launcher for the true console Python executable (allowing you to switch between v2 and v3 versions of Python with py -2 ... and py -3 ...), python.exe, so as a workaround you can try the following (use a full path to python.exe, if needed):
- Modify your
E:\a.ps1script to explicitly invoke your.pyscript withpython.exe:
python.exe E:\script.py
- Then launch your invisible PowerShell session as before, except with improved syntax:
# Short for:
# powershell.exe -WindowStyle Hidden -File E:\a.ps1
powershell -w h -f E:\a.ps1
If that doesn't help, consider reinstalling Python.
[1] I'm guessing this is because the windows potentially created by the .pyw scripts themselves are unrelated to the (invisible) main window of the pythonw.exe GUI-subsystem executable.
Solution 2:[2]
Start-Process python -ArgumentList "python-script-file-path" -NoNewWindow
Lots of excellent info here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | another victim of the mouse |
