Run Python scripts in PowerShell directly

I'm using Windows PowerShell. Let's say I have a script called test.py that prints a few things. If I do:

PS D:\>.\test.py

then it opens a CMD window which prints a few things and then closes. It's actually running the Python interpreter under CMD. If I do

PS D:\>python test.py

it acts like I'd expect it to, with the output appearing in PowerShell.

How can I make it so that the script will run in PowerShell when I just give its name?


Solution 1:

Edit the PATHEXT environment variable and add the .py extension.

Just add this line to your PowerShell profile:

$env:PATHEXT += ";.py"

or you could just edit PATHEXT globally in the system settings (just search in the Start menu for "environment" and choose the option for "Edit environment variables for your account").