How to Run a Python Script in PowerShell

To run a Python script in PowerShell, open PowerShell in the script’s folder and run Python with the file’s name:

python .\script.py

Replace script.py with your actual filename. The steps below cover checking your Python installation, running a saved file, and handling paths, virtual environments, background processes, and common errors.

How PowerShell Runs Python

PowerShell starts the Python interpreter; Python reads and executes your .py file. You do not need to convert the file into a PowerShell script or change PowerShell’s execution policy for an ordinary Python invocation.

The two commands you will commonly encounter on Windows are:

  • python: Starts the Python interpreter selected by your command configuration or active virtual environment.
  • py: Provides Python launcher and, with the Python install manager, runtime-management features.

Passing a filename runs that file. Starting Python without a filename opens its interactive interpreter instead.

If your prompt shows >>>, you are inside Python, not PowerShell. Return to PowerShell before entering terminal commands:

exit()

Although Windows file associations may let you launch a .py file directly, explicitly choosing the interpreter makes the behavior easier to predict and troubleshoot.

How to Run a Python File in PowerShell

Choose the interpreter based on the project you are running:

Your Situation Interpreter to Use
An ordinary script with no special environment requirements Your default Python interpreter
A project with its own virtual environment That environment’s Python executable
A script that requires a particular Python release A version-qualified Python launcher invocation

Step 1: Check or Install Python

  1. Open PowerShell or a PowerShell tab in Windows Terminal, then run `python –version` to check that Python starts.
  2. Check whether Python is available:
    python --version

    A Python version number means the command works. You do not need to reinstall Python simply to run a script.

  3. If Python is unavailable, install the Python install manager from python.org/downloads or the Microsoft Store app. This is the CPython team’s supported Windows installation route. With the manager’s default command setup and no active virtual environment, the global python command launches your current latest Python from any terminal, per Python’s Windows documentation. On Windows Server 2019, use the Python install manager’s MSI installer.
  4. After installation, close and reopen PowerShell so it picks up command and environment changes. Allow the manager to install a runtime if prompted, then verify that Python starts:
    python --version

You normally do not need an administrator PowerShell window to run a Python script.

Step 2: Open the Script’s Folder

  1. Change to the folder containing your file. Replace this example path with the actual folder:
    Set-Location -LiteralPath "C:\Users\YourName\Projects\MyApp"

    Quoting the path also handles folder names containing spaces.

  2. List the Python files in that folder:
    Get-ChildItem -File -Filter *.py

    Check the spelling of the file you want to run. If it is missing, you may be in the wrong folder or the file may have been saved with an extra extension.

  3. If you need a test file, create a new file named script.py in a text editor and save this code inside it:
    print("Hello from Python!")

    Make sure the filename is script.py, not script.py.txt. In File Explorer, turn on View > Show > File Name Extensions to check.

Step 3: Execute the File

  1. Run the script:
    python .\script.py

    The .\ means “in the current folder.” It makes the location explicit, although Python also accepts a bare filename.

  2. Read the output. For the test file, you should see:
    Hello from Python!
  3. Wait for the PowerShell prompt to return. A script that asks for input will wait for your response; a continuously running script may not return on its own. To interrupt a foreground Python process, press Ctrl+C.

Keep the terminal open if an error appears. The traceback usually identifies the file, line, and exception that need attention.

Handle Script Paths, Arguments, and Python Versions

Quoted paths, correctly placed arguments, and explicit runtime selection let PowerShell run the intended script with the intended Python interpreter.

Use a Full Script Path

You can run a file without first changing folders:

python "C:\Projects\My App\script.py"

The script’s location and PowerShell’s working folder are separate settings.

An absolute script path tells Python where to find the code; it does not change the process’s working directory. If the script opens a relative filename such as input.csv, it normally looks relative to PowerShell’s current folder, unless the code explicitly constructs another path.

For a project that expects its data files in the project folder, set that folder as the working directory before running it.

Use a Quoted Python Executable Path

If Python is not on your command search path, or you need a particular installation, invoke its executable directly. Replace both example paths with real ones:

& "C:\Tools\Python\python.exe" "C:\Projects\My App\script.py"

PowerShell’s call operator, &, executes the quoted program path. Without it, a quoted executable path is just a string.

Quote the executable and script paths separately. Do not put the entire command, including its arguments, inside one quoted string.

Pass Arguments to the Script

Place the script’s arguments after its filename:

python .\script.py --input ".\data files\sales.csv" --verbose

Here, --input and --verbose are example arguments that the script must support. PowerShell passes them to Python; it does not automatically add those features to your program.

Python interpreter options go before the script filename. Options intended for your script go after it.

Select a Specific Python Runtime

A default interpreter can change as runtimes are added or removed. For a project that requires a particular release, explicitly select that runtime using the version-qualified launcher supported by Python’s Windows documentation.

The listing and installation commands here require the Python install manager, not just the legacy Windows launcher.

First, check the available online runtime:

py list --online 3.15

Install the matching release if it is not already installed:

py install 3.15

Then run your script with that version:

py -V:3.15 .\script.py

This selects the requested minor version rather than whichever runtime is currently the default; it does not pin an exact patch release.

Only if your project specifically requires a free-threaded runtime, install the t variant:

py install 3.15t

Then select that variant when running the script:

py -V:3.15t .\script.py

Free-threaded Python is not required simply because you are using PowerShell.

Run a Script in a Python Virtual Environment

A virtual environment gives a project its own Python package installation area. I use a Python virtual environment here to keep the project’s packages separate from other projects.

From the project folder, use the virtual environment’s interpreter to install the project’s dependencies and run its script:

  1. Create the environment if the project does not already have one:
    python -m venv .venv
  2. If the project provides a requirements.txt file, install its dependencies using the environment’s interpreter:
    .\.venv\Scripts\python.exe -m pip install -r .\requirements.txt
  3. Run the script with that same interpreter:
    .\.venv\Scripts\python.exe .\script.py
  4. Check the script’s output and any files it is supposed to produce.

Activation is optional. Calling the environment’s executable directly selects its interpreter and installed packages without changing your shell session.

This also makes the package relationship explicit: the interpreter used to install dependencies is the one used to run the program. A separate, unqualified pip command can otherwise target a different Python installation.

Run Python in the Background From PowerShell

For a script that already works with your default Python, use Start-Process to launch a separate Windows process while keeping your PowerShell prompt available.

Open the folder containing script.py, then obtain the actual interpreter path:

$pythonExe = python -c "import sys; print(sys.executable)"

Start the process and redirect its output to separate log files:

$process = Start-Process -FilePath $pythonExe `
    -ArgumentList '"script.py"' `
    -WorkingDirectory (Get-Location).Path `
    -WindowStyle Hidden `
    -RedirectStandardOutput ".\python.stdout.log" `
    -RedirectStandardError ".\python.stderr.log" `
    -PassThru

Remove the blank lines between the continuation lines. Copy the whole block, and do not add spaces after the backticks.

The explicit working directory preserves the project’s relative-path behavior. The hidden window and redirected output suit a noninteractive script; do not use this setup for a program that needs you to answer terminal prompts.

Choose different log filenames if you need to preserve logs from an earlier run.

You can continue working in PowerShell while the process runs. When you want to wait for it to finish, use:

$process.WaitForExit()

Then inspect standard output:

Get-Content .\python.stdout.log

And standard error:

Get-Content .\python.stderr.log

Check the expected output files as well as the logs before treating the run as successful. This is a manual background launch, not a setup that automatically restarts the program after a reboot or failure.

Fix Common PowerShell and Python Errors

Command checks, path tests, correct dependencies, and direct virtual-environment invocation resolve common PowerShell and Python errors.

Python Is Not Recognized or Opens the Microsoft Store

If PowerShell does not recognize python or opens the Microsoft Store instead, troubleshoot Python’s command configuration or installation rather than the script.

Check what PowerShell finds:

Get-Command python -All

If you recently installed Python, close and reopen PowerShell first. If a Python install manager installation still sends you to the Store, open Manage App Execution Aliases from Windows search and check which application owns the Python aliases.

Enable the install manager’s Python aliases and resolve any competing Python aliases. Do not assume that a path containing WindowsApps is itself wrong: the install manager also uses Windows app aliases.

Verify the interpreter after correcting the command setup:

python -c "import sys; print(sys.executable)"

A real interpreter path confirms that Python started. Then rerun your script:

python .\script.py

Python Cannot Open the Script File

An error containing “can’t open file” or “No such file or directory” means Python started but could not find the script at the supplied path.

Test the filename:

Test-Path -LiteralPath .\script.py

If the result is False, correct the folder or filename. Check for a hidden .txt extension, and distinguish the script’s path from any data-file paths it uses.

Once the path exists, run it:

python .\script.py

If the script starts and then reports a missing data file, troubleshoot its working directory or the path constructed inside the code instead.

A Package Cannot Be Imported

ModuleNotFoundError means Python ran but could not import a module. A missing dependency or the wrong interpreter is a common cause.

For a project that provides requirements.txt, install the declared dependencies with the interpreter you intend to use:

python -m pip install -r .\requirements.txt

Then run the project with that interpreter:

python .\script.py

If the project uses a virtual environment, invoke its Python executable for both operations. If there is no requirements file, use the project’s dependency instructions rather than guessing from the import name: a module’s import name and its installable package name are not always identical.

PowerShell Says Running Scripts Is Disabled

If the error names Activate.ps1, PowerShell is blocking the virtual environment’s activation script, not Python from interpreting your .py file.

With an existing .venv environment, run its interpreter directly:

.\.venv\Scripts\python.exe .\script.py

No execution-policy change is needed for this invocation. This avoids weakening a PowerShell setting merely to select a Python environment.

Confirm the Script Ran Correctly

Immediately after a foreground Python command finishes, inspect its exit code:

$LASTEXITCODE

An exit code of 0 conventionally indicates successful termination. A nonzero code usually indicates an error or an explicit failure status chosen by the script.

A successful launch is not the same as a successful result.

Confirm that:

  • The terminal shows the expected output, without an unexpected traceback.
  • Any expected files were created in the intended location.
  • The files or other results contain the expected data.

A script can finish successfully without printing anything, so silence alone is not an error.

For repeatable runs, record the required Python version and project dependencies. Once your test file works, run the actual project script with its intended interpreter and check the result it was written to produce.

Frequently Asked Questions

How Do I Install Python Using PowerShell on Windows?

If WinGet is available, locate the Python install manager in the Microsoft Store catalog:

winget search "Python install manager" --source msstore

Copy the ID for the matching Python install manager result, paste it at this prompt, and install that exact package:

$managerId = Read-Host "Python install manager package ID"
winget install --id $managerId --exact --source msstore

Accept the installation prompts, close and reopen PowerShell, and confirm that Python starts:

python --version

If WinGet is unavailable, install the Python install manager through the Microsoft Store app instead.

What Does Python’s -u Option Do in PowerShell?

Python’s -u option makes standard output and standard error unbuffered, which helps messages appear promptly when output is redirected to logs or another process:

python -u .\script.py

It does not change standard input or run the script in the background. The option belongs to Python, so it goes before the script filename.

How Do I Launch Python From CMD?

Command Prompt can run a Python script with the same basic interpreter-and-filename syntax:

python .\script.py

Run it from the script’s folder, or supply the script’s full quoted path. PowerShell-specific features such as the & call operator, Set-Location, and Start-Process are not CMD commands.