This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Visual Studio Code (VS Code) is a cross-platform script editor by Microsoft. Together withthePowerShell extension, it provides a rich and interactive script editing experience,making it easier to write reliable PowerShell scripts. Visual Studio Code with the PowerShellextension is the recommended editor for writing PowerShell scripts.
It supports the following PowerShell versions:
Note
Visual Studio Code isn't the same asVisual Studio.
Before you begin, make sure PowerShell exists on your system. For modern workloads on Windows,macOS, and Linux, see the following links:
For traditional Windows PowerShell workloads, seeInstalling Windows PowerShell.
Important
TheWindows PowerShell ISE is still available for Windows. However, it's no longer inactive feature development. The ISE only works with PowerShell 5.1 and older. As a component ofWindows, it continues to be officially supported for security and high-priority servicing fixes.we've no plans to remove the ISE from Windows.
Install Visual Studio Code. For more information, see the overviewSetting up Visual Studio Code.
There are installation instructions for each platform:
Install the PowerShell Extension.
code
in a console orcode-insiders
if you installedVisual Studio Code Insiders.ext install powershell
and pressEnter.For example, to create a new file, clickFile > New. To save it, clickFile > Save and thenprovide a filename, such asHelloWorld.ps1
. To close the file, click theX
next to the filename.To exit VS Code,File > Exit.
Some systems are set up to require validation of all code signatures. You may receive the followingerror:
Language server startup failed.
This problem can occur when PowerShell's execution policy is set by Windows Group Policy. Tomanually approve PowerShell Editor Services and the PowerShell extension for VS Code, open aPowerShell prompt and run the following command:
Import-Module $HOME\.vscode\extensions\ms-vscode.powershell*\modules\PowerShellEditorServices\PowerShellEditorServices.psd1
You're prompted withDo you want to run software from this untrusted publisher? TypeA
to runthe file. Then, open VS Code and verify that the PowerShell extension is functioning properly. Ifyou still have problems getting started, let us know in aGitHub issue.
With PowerShell installing side-by-side with Windows PowerShell, it's now possible to use a specificversion of PowerShell with the PowerShell extension. This feature looks at a few well-known paths ondifferent operating systems to discover installations of PowerShell.
Use the following steps to choose the version:
If you installed PowerShell to a non-typical location, it might not show up initially in the SessionMenu. You can extend the session menu byadding your own custom paths as described below.
The PowerShell session menu can also be accessed from the{}
icon in the bottom right corner ofstatus bar. Hovering on or selecting this icon displays a shortcut to the session menu and a smallpin icon. If you select the pin icon, the version number is added to the status bar. The versionnumber is a shortcut to the session menu requiring fewer clicks.
Note
Pinning the version number replicates the behavior of the extension in versions of VS Code before1.65. The 1.65 release of VS Code changed the APIs the PowerShell extension uses and standardizedthe status bar for language extensions.
First, if you're not familiar with how to change settings in VS Code, we recommend readingVisual Studio Code's settings documentation.
After reading the documentation, you can add configuration settings insettings.json
.
{ "editor.renderWhitespace": "all", "editor.renderControlCharacters": true, "files.trimTrailingWhitespace": true, "files.encoding": "utf8bom", "files.autoGuessEncoding": true}
If you don't want these settings to affect all files types, VS Code also allows per-languageconfigurations. Create a language-specific setting by putting settings in a[<language-name>]
field. For example:
{ "[powershell]": { "files.encoding": "utf8bom", "files.autoGuessEncoding": true }}
Tip
For more information about file encoding in VS Code, seeUnderstanding file encoding. Also, check outHow to replicate the ISE experience in VS Code for other tips on how to configure VSCode for PowerShell editing.
You can add other PowerShell executable paths to the session menu through theVisual Studio Code setting:powershell.powerShellAdditionalExePaths
.
You can do this using the GUI:
You can add as many additional paths as you like. The added items show up in the session menu withthe given key as the name.
Alternatively you can add key-value pairs to the objectpowershell.powerShellAdditionalExePaths
in yoursettings.json
:
{ "powershell.powerShellAdditionalExePaths": { "Downloaded PowerShell": "C:/Users/username/Downloads/PowerShell/pwsh.exe", "Built PowerShell": "C:/Users/username/src/PowerShell/src/powershell-win-core/bin/Debug/net6.0/win7-x64/publish/pwsh.exe" },}
Note
Prior to version 2022.5.0 of the extension, this setting was a list of objects with the requiredkeysexePath
andversionName
. A breaking change was introduced to support configuration viaGUI. If you had previously configured this setting, please convert it the new format. The valuegiven forversionName
is now theKey, and the value given forexePath
is now theValue. You can do this more easily by resetting the value and using the Settings interface.
To set the default PowerShell version, set the valuepowershell.powerShellDefaultVersion
to thetext displayed in the session menu (the text used for the key):
{ "powershell.powerShellAdditionalExePaths": { "Downloaded PowerShell": "C:/Users/username/Downloads/PowerShell/pwsh.exe", }, "powershell.powerShellDefaultVersion": "Downloaded PowerShell",}
After you've configured this setting, restart VS Code or to reload the current VS Code window fromtheCommand Palette, typeDeveloper: Reload Window
.
If you open the session menu, you now see your additional PowerShell installations.
Tip
If you build PowerShell from source, this is a great way to test out your local build ofPowerShell.
In VS Code version 1.9 (or higher), you can debug PowerShell scripts without opening the folder thatcontains the PowerShell script.
You should see the Debug actions pane appear that allows you to break into the debugger, step,resume, and stop debugging.
Workspace debugging refers to debugging in the context of a folder that you've opened from theFile menu usingOpen Folder.... The folder you open is typically your PowerShell projectfolder or the root of your Git repository. Workspace debugging allows you to define multiple debugconfigurations other than just debugging the currently open file.
Follow these steps to create a debug configuration file:
Open theDebug view on Windows or Linux by pressingCtrl+Shift+D. On macOS, pressCmd+Shift+D.
Click thecreate a launch.json file link.
From theSelect Environment prompt, choosePowerShell.
Choose the type of debugging you'd like to use:
VS Code creates a directory and a file.vscode\launch.json
in the root of your workspace folder tostore the debug configuration. If your files are in a Git repository, you typically want to committhelaunch.json
file. The contents of thelaunch.json
file are:
{ "version": "0.2.0", "configurations": [ { "type": "PowerShell", "request": "launch", "name": "PowerShell Launch (current file)", "script": "${file}", "args": [], "cwd": "${file}" }, { "type": "PowerShell", "request": "attach", "name": "PowerShell Attach to Host Process", "processId": "${command.PickPSHostProcess}", "runspaceId": 1 }, { "type": "PowerShell", "request": "launch", "name": "PowerShell Interactive Session", "cwd": "${workspaceRoot}" } ]}
This file represents the common debug scenarios. When you open this file in the editor, you see anAdd Configuration... button. You can click this button to add more PowerShell debugconfigurations. One useful configuration to add isPowerShell: Launch Script. With thisconfiguration, you can specify a file containing optional arguments that are used whenever you pressF5 no matter which file is active in the editor.
After the debug configuration is established, you can select the configuration you want to useduring a debug session. Select a configuration from the debug configuration drop-down in theDebug view's toolbar.
If you experience any issues using VS Code for PowerShell script development, see thetroubleshooting guide on GitHub.
There are a few videos and blog posts that may be helpful to get you started using the PowerShellextension for VS Code:
The PowerShell extension's source code can be found onGitHub.
If you're interested in contributing, Pull Requests are greatly appreciated. Follow along with thedeveloper documentation on GitHub to get started.
Was this page helpful?
Was this page helpful?