You can use PowerShell ISE or Visual Studio Code to run a PowerShell script.
To run a PowerShell script using VS code, you need to first install the PowerShell extension in Visual Studio Code.
You can do this by opening the Extensions view using the keyboard shortcut Ctrl+Shift+X, typing “PowerShell” in the search box, and then clicking the Install button next to the PowerShell extension.
Once the extension is installed, you can easily write and run PowerShell scripts.
Open a new file, save it with a .ps1 extension, and start writing your script.
To execute your script, select the code you want to run, press Ctrl+Shift+P, and type “run selected” to find the Terminal: Run Selected Text In Active Terminal command.
Overview of Visual Studio Code
Visual Studio Code (VSCode) is a popular code editor developed by Microsoft. It is known for its lightweight design and robust functionality, making it suitable for various programming tasks.
For instance, enabling the files.trimTrailingWhitespace
setting automatically removes excess whitespace from the file, keeping the code clean.
The integrated terminal is another standout feature. It allows developers to run commands directly within VSCode.
This terminal can be accessed by pressing `Ctrl+“ or navigating the menu.
VSCode offers a command palette, brought up by pressing Ctrl+Shift+P
, which provides access to numerous functionalities, including running selected text in the terminal.
Files and projects are organized in a sidebar, making navigation straightforward.
The editor supports various programming languages, syntax highlighting, and debugging capabilities.
The console feature allows for debugging output, making tracking and resolving issues during development easier.

To run PowerShell scripts in Visual Studio Code, you first need to install Visual Studio Code on your system.
Downloading Visual Studio Code
- Visit the Visual Studio Code website:
- For Windows, Linux, and macOS, there are specific download links.
Install on Windows
- Download the installer: Make sure to download the setup for your version of Windows.
- Run the installer: Follow the on-screen instructions to complete the installation.
- Launch Visual Studio Code: After installation, open Visual Studio Code from the Start menu or desktop shortcut.
Install on macOS
- Download the .dmg file from the Visual Studio Code website.
- Open the downloaded file: Drag the Visual Studio Code icon to the Applications folder.
- Open Visual Studio Code: Launch it from the Applications folder or by using Spotlight Search.
Install on Linux
- Debian-based distributions:
- Download the .deb package.
- Open a terminal and run:
sudo apt install ./<file>.deb
- Red Hat-based distributions:
- Download the .rpm package.
- Open a terminal and run:
sudo rpm -i <file>.rpm
Installing PowerShell
After installing Visual Studio Code, install PowerShell to run scripts. PowerShell 7.2 supports Windows, Linux, and macOS:
- Install PowerShell Core:
- Download packages from the PowerShell GitHub releases page.
- Follow platform-specific instructions to complete installation.
- Verify installation: Open a terminal or command prompt and run:
pwsh
This command should start PowerShell if installed correctly.
Read PowerShell Data Types
Configure Visual Studio Code for PowerShell
Setting up Visual Studio Code for PowerShell involves installing the PowerShell extension and configuring the integrated environment. This setup provides features like IntelliSense, debugging, syntax highlighting, and a built-in console.
Utilizing the PowerShell Extension
To start, install the PowerShell extension from the Visual Studio Code Marketplace.
Open Visual Studio Code, click on the Extensions icon on the left sidebar or use the shortcut Ctrl+Shift+X. In the search box, type “PowerShell” and click the install button next to the extension.
This extension offers rich language support, such as IntelliSense for code completion, syntax highlighting for better readability, and debugging capabilities.
It also integrates with PowerShell Editor Services, enhancing the editing experience.
Once installed, restart Visual Studio Code to load the extension and its functionalities fully.
Set Up the Integrated Environment
After installing the PowerShell extension, configure the integrated console.
Navigate to File > Preferences > Settings. In the search box, type “terminal integrated shell” and update the terminal settings to set the default shell to PowerShell.
Next, open the keybindings.json file through File > Preferences > Keyboard Shortcuts and select keybindings.json to customize shortcuts for efficient workflow.
Add configurations for launching the PowerShell terminal directly within VS Code.
Configure language mode for PowerShell scripts to ensure proper linting and syntax checking.
Open a PowerShell script file, and an option to set the language mode appears in the bottom right of the screen. Select “PowerShell.”
How to Debug PowerShell Script using VS Code
Debugging PowerShell scripts in Visual Studio Code is straightforward. Begin by installing the PowerShell extension.
Open the Extensions view with Ctrl+Shift+X and search for “PowerShell”. Click Install next to the PowerShell extension.
To start debugging, open your PowerShell script. Set a breakpoint by clicking in the margin next to the line where you want the script to pause.
Breakpoints can be managed in various ways:
- Line Breakpoints: Click the margin to add a breakpoint on any line.
- Conditional Breakpoints: Right-click the breakpoint and add conditions like specific variable values.
Press F5 to launch the debugger. The script will run up to the breakpoint and pause.
Use the Debug View to inspect variables, call stacks, and watch expressions.
You can add variables to the watch list to track their values during the execution.
The Debug Console allows running commands interactively while paused.
Use commands like Get-Variable
to inspect and modify variables.
Use CodeLens to see references and function details directly above the code. This helps navigate large scripts efficiently.
For script analysis, install PSScriptAnalyzer. This tool provides linting and script analysis to catch potential issues.
How to Work with Command Palette and Keyboard Shortcuts
Using the Command Palette and keyboard shortcuts makes working with PowerShell scripts in Visual Studio Code efficient and straightforward.
For example, to enable PowerShell ISE Mode, type “ISE Mode” in the palette and select PowerShell: Enable ISE Mode.

Common Commands in the Command Palette
- Open a new terminal:
Terminal: Create New Integrated Terminal
- Run a selected script:
PowerShell: Run Selection
- Format document:
PowerShell: Format Document
Useful Keyboard Shortcuts
- Open Extensions View:
Ctrl+Shift+X
- Run Code:
F5
- Stop Running Code:
Shift+F5
- Toggle Terminal:
Ctrl+`
Editing and Navigation Shortcuts
- Copy Line Down:
Alt+Shift+Down
- Move Line Up:
Alt+Up
- Go to Definition:
F12
- Find in Files:
Ctrl+Shift+F
Read How to Run PowerShell Script From Command Line With Parameters?
Customize User and Workspace Settings
To access the Settings editor, navigate to File > Preferences > Settings.
Alternatively, press Ctrl+,
on Windows and Linux or Cmd+,
on macOS. This brings up a comprehensive interface for managing all configurations.
For quick changes, using the Command Palette is an efficient method.
Open it by pressing Ctrl+Shift+P
, and type “Preferences: Open Settings”. This method allows you to modify settings without leaving your code window.
Example Settings to Configure
- Font Size:
- Increase readability by adjusting font size.
"editor.fontSize": 14
- Theme:
- Customize the look and feel by choosing from various themes.
"workbench.colorTheme": "Visual Studio Dark"
- PowerShell Extension:
- Ensure PowerShell scripts have syntax highlighting and other features.
"powershell.codeFormatting.preset": "Custom"
Configuring the Status Bar
The Status Bar at the bottom can be customized to display important information. For example, you can show the currently active PowerShell version.
"powershell.integratedConsole.showOnStartup": true
Workspace Settings
Setting up workspaces allows project-specific configurations:
- Open or create a
.code-workspace
file. - Define settings that apply only to the current workspace.
{
"folders": [],
"settings": {
"powershell.codeFormatting.autoCorrectAliases": true
}
}
Testing PowerShell Scripts using Visual Studio Code
Testing PowerShell scripts in Visual Studio Code is easy.
First, ensure you have the PowerShell extension installed. This extension supports both PowerShell Core and Windows PowerShell 5.1.
To start, open your script file in Visual Studio Code.
For testing purposes, use a command like:
Write-Output "Hello, World!"
To debug, add breakpoints by clicking on the left margin next to the line number.
Steps to Test:
- Press
F5
to start debugging. - The script runs and stops at breakpoints, letting you inspect variables.
PowerShell Integrated Console within the editor provides an interactive experience, allowing you to run and test scripts in real time. Press Ctrl+Shift+
to open this console.
Read PowerShell try catch examples
Handle Execution Policy Restrictions in VS Code
To run PowerShell scripts in Visual Studio Code, it’s important to handle execution policy restrictions correctly. Here’s what you need to know.
Execution Policy is a safety feature in PowerShell that controls the conditions under which PowerShell loads configuration files and runs scripts. Visual Studio Code respects these settings.
To view current execution policies:
Get-ExecutionPolicy -List
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force
To persist settings for different PowerShell versions or editions, use different commands and PowerShell executables as required.
Here’s a table summarizing common execution policies:
Policy | Description |
---|---|
Restricted | No scripts can run. |
AllSigned | Only scripts signed by a trusted publisher can run. |
RemoteSigned | Downloaded scripts must be signed by a trusted publisher. |
Unrestricted | All scripts can run. |
Frequently Asked Questions
How do I execute a PowerShell script using Visual Studio Code’s integrated terminal?
To execute a PowerShell script, open your script file in Visual Studio Code. Right-click inside the file and select “Run PowerShell Script.” Alternatively, use the default shortcut: F5
. The script will run in the integrated terminal at the bottom of the window.
What steps are needed to run PowerShell scripts in Visual Studio Code on a Mac?
First, download and install Visual Studio Code for macOS. Open the app and go to the Extensions view (Cmd+Shift+X). Search for “PowerShell” and install the extension. After installation, you can open and run PowerShell scripts like Windows.
Where can I find and install the PowerShell extension for Visual Studio Code?
Open Visual Studio Code and go to the Extensions view by clicking the Extensions icon or using the shortcut Ctrl+Shift+X
. In the search box, type “PowerShell”. The PowerShell extension should appear in the search results. Click the “Install” button next to the extension.
Can you run a PowerShell script in VS Code with PowerShell 7 integration?
Yes, you can run PowerShell 7 scripts in Visual Studio Code. Make sure you have PowerShell 7 installed on your machine. Open Visual Studio Code, and ensure that the PowerShell extension is installed. The extension supports PowerShell 7 and integrates seamlessly.
How to configure Visual Studio Code to run a PowerShell script as an administrator?
Conclusion
I hope you know how to use Visual Studio code to execute PowerShell scripts.
This can be done by opening the Extensions view and searching for “PowerShell.”
After installing the extension, syntax highlighting will start working automatically when a PowerShell script file is saved.
Typing “run selected” will allow execution of the highlighted code in the active terminal.
If you still have any questions, feel free to leave a comment below.
You may also like: