2

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: What I don't want to show up

1
  • 1
    @mklement0 im using run (WIN + R) and this command: powershell -w h iex (E:\a.ps1) Commented Apr 9, 2022 at 5:03

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I launch a python script without having to specify python.exe? In other words, being able to execute a script called some.py, by only typing it's name?
@not2qubit, for that (a) $env:PATHEXT must have a .py entry and (b) a file association for .py must exist that defines the interpreter to use for such files. As far as I know, Python installers set that up automatically, but depending on your installation method it may not be set up.
Yes, I have already done that, and was looking at some other comments by Eryk Sun, and it seem that whatever was said before doesn't work anymore. (Using py3.12.1 on Win-11 Pro) I also made sure to install the py launcher & checking the paths, assoc, ftype etc.
@not2qubit: That's odd. I suggest asking a new question specifically about that.
-1
Start-Process python -ArgumentList "python-script-file-path" -NoNewWindow

Lots of excellent info here.

2 Comments

Start-Process -NoNewWindow is asking for trouble, because it asynchronously launches a console application in the current console, with its output arriving unpredictably in the current console. To run a Python script synchronously in the current console, just invoke it directly (or by passing its path as an argument to python.exe or py.exe with -2 or -3).
While you could add -Wait to ensure synchronous execution, there is no reason to use Start-Process to begin with in this case: Just invoke python "python-script-file-path" directly, which will also run in the current window, invariably synchronously, and, perhaps most importantly, with the Python script's output streams connected to PowerShell's streams. GitHub docs issue #6239 provides guidance on when use of Start-Process is and isn't appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.