Good to see you again. In the previous lesson, you ran hello.py through VS Code’s Run Python File in Terminal button. This time, you will launch that same script yourself from PowerShell. This is useful because it makes the relationship clear: PowerShell receives a command, and the Python launcher (py) runs the Python file you name.
By the end, you will be able to open PowerShell, move to your project folder, and run:
py .\hello.py
Plan for about 30–40 minutes, including a few repeat runs.
Script, interpreter, and terminal
Your hello.py file is a script: saved Python instructions that you can run repeatedly.
PowerShell is a Windows command-line application. It shows a prompt similar to this:
PS C:\Users\YourName>
The exact folder in your prompt will differ. The prompt is PowerShell telling you it is ready for a command. You type commands after it; you do not type the PS ...> part yourself.
The command you will use has two pieces:
py .\hello.py
| Piece | Meaning |
|---|---|
py | The Windows Python launcher. It starts the installed Python interpreter. |
.\hello.py | The script to run. .\ means “in the current folder,” and hello.py is the filename. |
When you press Enter, Python reads the saved contents of hello.py, executes them from top to bottom, and returns you to PowerShell when it finishes.
A key distinction: py .\hello.py is a PowerShell command, not Python code. Type it in a terminal, not in the hello.py editor.
Read the command-line workflow
The following Real Python section provides a useful view of the same workflow: a script is saved in a folder, PowerShell is opened in that folder, and the script name is given to py.
Interacting With Python – Real Python
Read “Running Python Scripts From a File” from Real Python to see the general command-line workflow before using it with your existing VS Code project.
In the section “Running Python Scripts From a File,” read the file workflow. Focus on the idea of the current working directory: when PowerShell is already in the folder containing the script, the filename is enough to identify what Python should run. The article uses py hello.py; in this lesson, py .\hello.py makes the current-folder location explicit.
Open PowerShell in your project folder
The simplest starting point is your existing python-beginnings folder, which contains hello.py.
Option A: Use VS Code’s PowerShell terminal
This is still PowerShell, even though it appears inside VS Code.
- Open the
python-beginningsfolder in VS Code. - Select Terminal from the top menu.
- Select New Terminal.
- Look at the prompt in the lower terminal panel.
Usually, VS Code opens the terminal in the folder currently open as your workspace. You may see something like:
PS C:\Users\YourName\Documents\python-beginnings>
To verify your current folder, type:
Get-Location
Then press Enter. PowerShell displays its current location.
Next, list the files in that location:
dir
You should find hello.py in the output. The listing may contain other folders and files too; that is normal.
Option B: Open PowerShell separately
You can also use PowerShell outside VS Code:
- Press the Windows key.
- Type PowerShell.
- Open Windows PowerShell or PowerShell.
- Change into the folder containing your script with
cd, short for “change directory.”
For example, if your project is in Documents:
cd "C:\Users\YourName\Documents\python-beginnings"
Replace YourName and the rest of the location with your real folder path. The quotation marks are helpful because Windows folder names sometimes contain spaces.
If you are unsure of the folder path, return to VS Code, right-click the python-beginnings folder in the Explorer panel, and choose Copy Path. Then paste that path between the quotation marks after cd.
Once you have changed folders, use:
dir
Seeing hello.py confirms that PowerShell is in the right place.
Run hello.py yourself
First, open hello.py in VS Code and make sure it contains a working line such as:
print("Hello from PowerShell!")
Save it with Ctrl+S.
Now click in the PowerShell terminal, type the following command, and press Enter:
py .\hello.py
You should see:
Hello from PowerShell!
followed by a new PowerShell prompt. A full session might look like this:
PS C:\Users\YourName\Documents\python-beginnings> py .\hello.py
Hello from PowerShell!
PS C:\Users\YourName\Documents\python-beginnings>
Your folder path will not be identical, but the program output should match the text in your print() call.
Why save before running?
Python runs the version stored on disk. If you change text in VS Code but do not save, PowerShell runs the earlier saved version instead.
Try the full edit-run-observe cycle:
-
Change the text in
hello.py:print("I can run Python from PowerShell.") -
Save with Ctrl+S.
-
Return to PowerShell.
-
Press the Up Arrow key to recall your previous command.
-
Press Enter to run it again.
The output should now be:
I can run Python from PowerShell.
Using the Up Arrow is a small but practical terminal habit: it brings back prior commands so you do not need to retype them.
If PowerShell cannot find the script
Errors are useful clues about which part of the process needs attention.
You see “can’t open file” or a message that hello.py cannot be found
PowerShell is probably in the wrong folder, or the filename differs from what you typed.
Run:
dir
Check whether the list actually contains hello.py.
- If it does not, use
cdto move to the folder where you saved the file. - If you see
hello.py.txt, the file has accidentally been saved as a text document rather than a Python file. Rename it to end in exactly.py. - Check spelling and capitalization carefully, even though Windows usually does not distinguish uppercase and lowercase filenames.
py is not recognized
Earlier in the module, you verified Python with py --version. If py now is not recognized, first close and reopen PowerShell, then try:
py --version
If this still fails, Python’s launcher may not be available in this terminal session. Do not try random downloads or commands; revisit the earlier installation check and confirm that Python is installed.
The old message appears
This almost always means the file was not saved after editing. Return to hello.py, press Ctrl+S, and run:
py .\hello.py
again.
You get red Python error text
The command may have worked correctly, but the Python code inside the file has an error. Restore this known-good version:
print("Hello from PowerShell!")
Save and run it again. If that works, the PowerShell side is working; the next task is checking the changed Python code.
Two ways to name a script
The preferred everyday method is to move into the project folder and use the short command:
py .\hello.py
You can also run a file from any folder by giving its complete path:
py "C:\Users\YourName\Documents\python-beginnings\hello.py"
For now, prefer the first method. It is shorter, easier to read, and encourages you to keep each small project’s files together.
Before finishing, confirm this checklist:
- PowerShell was open, either separately or in VS Code’s terminal panel.
-
dirshowedhello.py. - You ran
py .\hello.py. - The terminal displayed the text produced by your program.
- You changed, saved, and reran the script successfully.
You can now run a saved Python program in two ways: through VS Code’s Run button or directly from PowerShell with py .\hello.py. The core routine is always the same: save the script, identify its folder, run it, and inspect the result.
In the next lesson, you will use py without a filename to enter Python’s interactive shell, where you can evaluate small expressions immediately rather than saving a script first.
Can't find a good explanation? Sign up and we'll make it for you
Sign up