To run a PowerShell script in Visual Studio Code, install Microsoft’s PowerShell extension, open your .ps1 file and press F5. To run only a few lines, select them and press F8. You can also type the script path in the terminal:
.\Get-Report.ps1
Below I set up the extension, run a script with and without parameters, debug it with breakpoints, switch between PowerShell 7 and Windows PowerShell 5.1, and fix the execution policy errors that stop scripts from running.
Install the PowerShell extension in VS Code
VS Code doesn’t run PowerShell scripts well on its own. The free PowerShell extension from Microsoft adds the PowerShell Extension terminal, IntelliSense, PSScriptAnalyzer warnings and the debugger.
- Install Visual Studio Code from code.visualstudio.com and open it.
- Press Ctrl+P, type
ext install powershelland press Enter. - In the Extensions view, pick PowerShell by Microsoft and click Install.
- If the button changes to Reload, click it.
The extension works with PowerShell 7.2 or later and with Windows PowerShell 5.1. If you’re not sure what’s installed, check your PowerShell version first. Other add-ons worth having are in my list of VS Code extensions for PowerShell.
Create and save a PowerShell script
Click File > New File, paste your code and save it with a .ps1 extension. The extension only starts once VS Code sees a PowerShell file. Here’s the Get-Report.ps1 script I’ll use in this post:
param(
[string]$City = 'Austin',
[int]$Days = 7
)
$end = Get-Date '2026-10-12'
$start = $end.AddDays(-$Days)
"Sales report for $City"
"Period: {0:yyyy-MM-dd} to {1:yyyy-MM-dd}" -f $start, $end
Output:
Sales report for Austin
Period: 2026-10-05 to 2026-10-12
The param block gives the script two parameters with defaults. Run without arguments, it reports on Austin for the last seven days. If you’re new to parameters, see how to pass variables to a PowerShell script.
Run the whole script with F5
With the script open, press F5 or click the Run button (the play icon) at the top right of the editor.
F5 starts the script under the debugger in the PowerShell Extension terminal at the bottom of the window. If there are no breakpoints, it simply runs to the end and prints the output there.
To pass arguments, type the call in that terminal instead. The leading .\ tells PowerShell to run the file from the current folder:
.\Get-Report.ps1 -City Denver -Days 30
Output:
Sales report for Denver
Period: 2026-09-12 to 2026-10-12
Denver and 30 days override the defaults. The same syntax works in any console, as shown in running a script from the command line with parameters.
How do I run selected lines in VS Code?
Select one or more lines and press F8, or right-click and choose Run Selection. With nothing selected, F8 runs the line the cursor is on. That’s the same key PowerShell ISE uses.
F8 sends the code to the PowerShell Extension terminal, not a fresh process. Variables and functions from earlier runs stay in that session, which is handy for testing step by step but can hide bugs.
If a script works only because a variable was left over from a previous run, open a new terminal or clear the variables and run it again.
Pass arguments with launch.json
F5 runs the script without arguments. To press F5 and still pass parameters, open the folder (File > Open Folder), press Ctrl+Shift+D, click create a launch.json file and pick PowerShell. Then add your values to args:
{
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch (current file)",
"script": "${file}",
"args": ["-City", "Denver", "-Days", "30"],
"cwd": "${file}"
}
]
}
VS Code saves this as .vscode\launch.json in the folder, so it’s worth committing with the project. Pick the configuration in the Run and Debug view and press F5.