0% found this document useful (0 votes)
5 views

How to use PowerShell with Visual Studio Code

This document provides a comprehensive guide on how to use PowerShell with Visual Studio Code, including installation of the PowerShell extension and its features. It outlines the steps for creating, debugging, and running PowerShell scripts within the editor, as well as additional functionalities like IntelliSense and PSScriptAnalyzer integration. The guide emphasizes the productivity benefits of using PowerShell in VS Code for developers and system administrators.

Uploaded by

Bogdan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

How to use PowerShell with Visual Studio Code

This document provides a comprehensive guide on how to use PowerShell with Visual Studio Code, including installation of the PowerShell extension and its features. It outlines the steps for creating, debugging, and running PowerShell scripts within the editor, as well as additional functionalities like IntelliSense and PSScriptAnalyzer integration. The guide emphasizes the productivity benefits of using PowerShell in VS Code for developers and system administrators.

Uploaded by

Bogdan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

How to use PowerShell with Visual Studio

Code?
January 15, 2023 Salaudeen Rajack

Requirement: Run PowerShell scripts from Visual Studio Code.

How to use PowerShell in Visual Studio Code?


Visual Studio Code is a powerful code editor from Microsoft, that supports a wide variety of
programming languages and tools, including PowerShell. This guide will show you how to
install the PowerShell extension and use it to work with PowerShell scripts and commands in
Visual Studio Code.

Assuming you have Visual Studio Code already downloaded and installed on your computer,
To use PowerShell with Visual Studio Code, you will need to install the PowerShell
extension for VS Code. Here are the basic steps to get started:

1. Open Visual Studio Code.


2. File >> Preferences >> Extensions (Shortcut key: Ctrl+Shift+X). Search and select
“PowerShell” in the Extensions pane.

3. Click the “Install” button for the “PowerShell” extension, and then click the “Reload”
button to enable the extension.
Once the extension is installed, you can start using PowerShell in Visual Studio Code. Here
are a few ways to use PowerShell in the editor:

 Create and debug PowerShell scripts: You can start writing your PowerShell scripts in
Visual Studio Code. It supports Intellisense, set breakpoints, and debug the script.
 Open a PowerShell file: You can open a PowerShell script file in Visual Studio Code
and edit it like any other code file.
 Run PowerShell commands: You can run PowerShell commands in the editor by
highlighting the command and run selection.

You can enable the PowerShell ISE experience in Visual Studio Code by turning on “ISE
Mode”. To do this, open the command palette (Shortcut key: F1) and type in “ISE Mode”.
Select “PowerShell: Enable ISE Mode” from the list.

Now, You have the full power of PowerShell in your VS Code editor, and you can use it to
automate tasks, manage your local and remote systems, create and manage scripts and much
more. The Visual Studio Code extension for PowerShell enables developers to write, test, and
debug PowerShell scripts from within the Visual Studio Code environment. This extension
includes features such as IntelliSense, which provides code completion and validation for
PowerShell functions, cmdlets, and modules. It also provides debugging capabilities for
PowerShell scripts and allows developers to create projects with multiple scripts.
In summary, using PowerShell in Visual Studio Code can greatly enhance your productivity
and efficiency as a developer or system administrator. The PowerShell extension for Visual
Studio Code provides seamless integration of the PowerShell command-line shell and
scripting language within the VS Code editor. With this extension, you can easily open and
edit PowerShell scripts, start new PowerShell sessions, and run PowerShell commands
directly within the VS Code interface. With this guide, you have learned how to install the
PowerShell extension and use it to work with PowerShell scripts and commands in Visual
Studio Code. Now, you can take advantage of the powerful features of PowerShell and
automate your tasks and manage your systems effectively in VS Code.

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 (VS Code) provides rich
language support and capabilities such as syntax completions, definition tracking, and linting
for PowerShell. The extension works anywhere you can run VS Code and PowerShell 7 or
higher. The extension also works for Windows PowerShell 5.1. The extension no longer
supports the older versions of Windows PowerShell.

Our test matrix includes the following configurations:

 Windows Server 2022 with Windows PowerShell 5.1 and PowerShell 7.2
 Windows Server 2019 with Windows PowerShell 5.1 and PowerShell 7.2
 macOS 11 with PowerShell Core 7.2
 Ubuntu 20.04 with PowerShell Core 7.2

Installing the PowerShell extension


There are two versions of the extension:

 PowerShell - the fully tested stable release


 PowerShell Preview - a preview release containing new features and other changes, but not
fully tested

Both extensions can be installed, but only one should be enabled. This allows you to switch
from the stable to the preview version to try out new features.

Installing from within VS Code

The PowerShell extension can be installed from the Visual Studio Code Marketplace by
clicking the Install Button. You can also install the PowerShell extension from within VS
Code by opening the Extensions view with keyboard shortcut Ctrl+Shift+X, typing
"PowerShell", and selecting the PowerShell extension:
Installing from the command line

The extension can also be installed from any command-line shell (including PowerShell,
cmd, and bash) on any supported platform using the following command:

code --install-extension ms-vscode.powershell

If you are running the VS Code Insiders build, use this command instead:

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

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 F8
 Launch online help for the symbol under the cursor using Ctrl + F1
 Local script debugging and basic interactive console support
 Enable ISE mode using Ctrl+Shift+P then search for "Enable ISE Mode"
Debugging

The PowerShell extension uses the built-in debugging interface of VS Code to allow for
debugging of PowerShell scripts and modules. For more information about debugging
PowerShell, see Using VS Code.

Multi-version support

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

Or run the PowerShell: Show Session Menu command from the Command Palette
(Ctrl+Shift+P).

CodeLens support

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.

CodeLens features include:

 Pester Run tests and Debug tests.

 Pester symbol support


 Function reference

CodeLens function reference support shows the number of times a function is


referenced within your code and allows you to jump to specific references.

PSScriptAnalyzer integration

PSScriptAnalyzer is a PowerShell module that provides a static source code checker for
modules and scripts. PSScriptAnalyzer has rules that verify the quality of PowerShell code.
These rules are based on PowerShell best practices identified by the PowerShell Team and
the community. PSScriptAnalyzer 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 you edit in VS Code.

PSScriptAnalyzer comes with a collection of built-in rules that check various aspects of
PowerShell source code such as presence of uninitialized variables, usage of PSCredential
type, usage of Invoke-Expression, and others. The module also allows you to include or
exclude specific rules.

To disable PSScriptAnalyzer, open your settings (Ctrl+,), browse Extensions, select the
PowerShell extension, and deselect the checkbox for Script Analysis: Enable
(powershell.scriptAnalysis.enable).

PSScriptAnalyzer also provides code formatting. You can invoke automatic document
formatting with the Format Document command or the (Shift+Alt+F) keyboard shortcut.

Pester integration

Pester is a framework for running unit tests to execute and Windows PowerShell 5.1 comes
with Pester 3.40 pre-installed. To update Pester or to install the latest version on other
platforms follow the Pester installation instructions.

Plaster integration

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.

The PowerShell extension allows the creation of new Plaster projects using the PowerShell:
Create New Project from Plaster Template command from the Command Palette
(Ctrl+Shift+P).

PowerShell extension settings


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

You can also select the gear icon located in the lower left corner of the Activity Bar.
You can also use the keyboard shortcut Ctrl+, to open your settings. You can still open the
settings.json file by using Preferences: Open Settings (JSON) command from the
Command Palette (Ctrl+Shift+P) 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.

Types.ps1xml and Format.ps1xml files

PowerShell .ps1xml files are used 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:

"xml.fileAssociations": [
{
"systemId":
"https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/
Schemas/Format.xsd",
"pattern": "**/*.Format.ps1xml"
},
{
"systemId":
"https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/
Schemas/Types.xsd",
"pattern": "**/*.Types.ps1xml"
}
]
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

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

~/.vscode/extensions/ms-vscode.PowerShell-<version>/examples

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

code (Get-ChildItem ~\.vscode\extensions\ms-vscode.PowerShell-*\examples)[-


1]

You can also open the examples from the Command Palette (Ctrl+Shift+P) with the
PowerShell: Open Examples Folder command.

Additional resources
More detailed documentation can be found in the PowerShell documentation. Start with
Using VS Code.

Check out the troubleshooting guide for answers to common questions.

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

You might also like