In a comment you state:
im using run (WIN + R) and this command: powershell -w h iex (E:\a.ps1)
-w h
is short for -WindowStyle Hidden
iex
is short for Invoke-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.exe
must 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 .ps1
scripts) 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-Process
run visibly.
- While you can generally use
Start-Process -WindowStyle Hidden
to run GUI applications invisibly as well, this, unfortunately, does not seem to work with .pyw
files, i.e. pythonw.exe
-launched scripts, which still run visibly.[1]
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.ps1
script to explicitly invoke your .py
script with python.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.