PowerShell in Visual Studio Code

PowerShell is a task-based command-line shell and scripting language built on .NET, which provides a powerful toolset for administrators on any platform.

The Microsoft PowerShell extension for Visual Studio Code provides rich language support and capabilities such as completions, definition tracking, and linting analysis for PowerShell versions 3, 4, 5, and 5.1 as well as all versions of PowerShell Core.

Install the PowerShell extension

The official PowerShell extension can be installed by following the steps described in the Visual Studio Code User Guide or by going directly to the Visual Studio Code Marketplace and clicking the Install Button.

You can also install the PowerShell extension from within Visual Studio Code by opening the Extensions view with keyboard shortcut kb(workbench.view.extensions) and typing “PowerShell” and select the PowerShell extension:

PowerShell extension

Install from the command line

Alternatively, the PowerShell extension can be installed from any command line (including PowerShell, Cmd, bash) on all platforms using the following command

  1. code --install-extension ms-vscode.powershell

If you are running VS Code Insiders, you will need this command instead:

  1. code-insiders --install-extension ms-vscode.powershell

Example scripts

Example scripts are included with the extension and can be found at the following path.

  1. C:\Users\<yourusername>\.vscode\extensions\ms-vscode.PowerShell-<version>\examples

To open or view the examples in Visual Studio Code, run the following from your PowerShell command prompt:

  1. code (Get-ChildItem $Home\.vscode\extensions\ms-vscode.PowerShell-*\examples)[-1]

If using the Insiders edition:

  1. code-insiders (Get-ChildItem $Home\.vscode-insiders\extensions\ms-vscode.PowerShell-*\examples)[-1]

You can also open the examples from the Command Palette (kb(workbench.action.showCommands)) with the PowerShell: Open Examples Folder command.

Open PowerShell Examples

Major features

  • Syntax highlighting
  • Code snippets
  • IntelliSense for cmdlets and more
  • Rule-Based analysis provided by PowerShell Script Analyzer
  • “Go to definition” of cmdlets and variables
  • Find references of cmdlets and variables
  • Document and Workspace symbol discovery
  • Run selected section of PowerShell code using kbstyle(F8)
  • Launch online help for the symbol under the cursor using kbstyle(Ctrl + F1)
  • Local script debugging and basic interactive console support

Debugging

The PowerShell extension uses the built-in debugging interface of VS Code to allow for debugging of PowerShell scripts and modules. You can learn more about VS Code debugging here.

Hey, Scripting Guy! blog

HSG

For more information on debugging, check out the “Hey, Scripting Guy!” two-part blog post series written by @keithHill on debugging with the PowerShell extension:

Debugging PowerShell script in Visual Studio Code – Part 1

Debugging PowerShell script in Visual Studio Code – Part 2

PowerShell extension settings

You can customize VS Code settings from the File > Preferences > Settings menu item (Code > Preferences > Settings on macOS).

You can also click the gear icon located in the lower left corner of the Activity Bar.

codeGear

You can also use the keyboard shortcut kb(workbench.action.openSettings) to open your settings. The VS Code team has introduced a settings GUI in version 1.27.1 as the new default interface for customizing settings. In VS Code versions prior to 1.27.1, this will open your settings.json file.

You can still open the settings.json file by using Preferences: Open Settings (JSON) command from the Command Palette (kb(workbench.action.showCommands)) or by changing the default settings editor with the "workbench.settings.editor" setting.

Go to User and Workspace settings for more information on configuring VS Code settings.

Multi-version support

You can configure the PowerShell extension to use any version of PowerShell installed on your machine by following these instructions.

You can also change the version by clicking on the version number in the lower right corner:

Change PowerShell Version

Or run the PowerShell: Show Session Menu command from the Command Palette (kb(workbench.action.showCommands)).

Pester

Pester is a Behavior-driven Development (BDD) based unit test runner for PowerShell. Pester provides a framework for running unit tests to execute and validate PowerShell commands. Pester follows a file naming convention for naming tests to be discovered by Pester at test time and a simple set of functions that expose a testing domain-specific language (DSL) for isolating, running, evaluating and reporting the results of PowerShell commands.

Windows 10 and Server 2016 comes with Pester 3.40 pre-installed. To update Pester or to install the latest version on other platforms follow the Pester installation instructions.

Pester CodeLens Integration

Plaster

Plaster is a template-based file and project generator written in PowerShell. Its purpose is to streamline the creation of PowerShell module projects, Pester tests, DSC configurations and more. See the GitHub Plaster repository for more information, for documentation on Plaster’s cmdlets see Cmdlet Documentation.

The PowerShell extension allows the creation of new Plaster projects by running the PowerShell: Create New Project from Plaster Template command from the Command Palette (kb(workbench.action.showCommands)).

Plaster Project

PSScriptAnalyzer

PSScriptAnalyzer is a static source code checker for PowerShell modules and scripts. PSScriptAnalyzer checks the quality of PowerShell code by running a set of rules. The rules are based on PowerShell best practices identified by the PowerShell Team and the community. Pester generates diagnostic records (errors and warnings) to inform users about potential code defects and suggests possible solutions for improvements.

The PowerShell extension includes PSScriptAnalyzer by default, and automatically performs analysis on PowerShell script files being edited in VS Code.

PSScriptAnalyzer Settings

PSScriptAnalyzer is shipped with a collection of built-in rules that checks various aspects of PowerShell source code such as presence of uninitialized variables, usage of PSCredential Type, usage of Invoke-Expression, etc. Additional functionality such as exclude/include specific rules is also supported.

To disable PSScriptAnalyzer, open your settings, browse Extensions, select the PowerShell extension and deselect the checkbox for PSScriptAnalyzer.

PSScriptAnalyzer Settings

Format document command is provided by the PSScriptAnalyzer module.

Document Formatting

Automatic document formatting can be invoked using the Format Document command or the (kb(editor.action.formatDocument)) keyboard shortcut.

CodeLens

CodeLenses are a VS Code feature to provide actionable, contextual information that is displayed within the source code.

CodeLens support was added in version 1.3.0 of the PowerShell extension, read the PowerShell extension changelog for more information.

Pester CodeLens support

Pester supports CodeLens integration for Run tests and Debug tests.

CodeLens Pester debug support

CodeLens Pester Debug Support

CodeLens Pester symbol support

CodeLens Pester Symbol Support

Function reference CodeLens support

CodeLens function reference support shows the number of times a function is referenced within your code and allows you to jump to specific references.

CodeLens Function Reference Support

Extension FAQ page

Check out the FAQ page on the PowerShell extensions Wiki

Types.ps1xml and Format.ps1xml files

ps1xml files are PowerShell’s way to extend the type system and define output formatting. For more information on these files, please refer to the official PowerShell documentation on Types.ps1xml and Format.ps1xml. You can get IntelliSense features when authoring ps1xml files by installing the XML extension by Red Hat. After installing, add this configuration to your user settings:

  1. "xml.fileAssociations": [
  2. {
  3. "systemId": "https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Format.xsd",
  4. "pattern": "**/*.Format.ps1xml"
  5. },
  6. {
  7. "systemId": "https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Types.xsd",
  8. "pattern": "**/*.Types.ps1xml"
  9. }
  10. ]

This tells the XML extension to use the official XML schemas from the PowerShell repository for all .ps1xml files. This enables the following features in ps1xml files:

  • Syntax error reporting
  • Schema validation
  • Tag and attribute completion
  • Auto-close tags
  • Symbol highlighting
  • Document folding
  • Document symbols and outline
  • Renaming support
  • Document Formatting