December 5th, 2016

Get started with PowerShell development in Visual Studio Code

Doctor Scripto
Scripter

Summary: Learn about the features in Visual Studio Code that help you write PowerShell code.

Since the PowerShell integrated scripting environment (ISE) shipped with Windows PowerShell 2.0 in 2009, the PowerShell landscape has changed significantly. In November 2014, Microsoft took .NET open source and cross-platform. Given PowerShell’s dependence on .NET, this was an important prerequisite before taking PowerShell cross-platform. And, indeed, nine months later, Microsoft made PowerShell available cross-platform, supporting Linux and macOS in addition to Windows. To the surprise of many, Microsoft also open-sourced PowerShell. Did I mention that Visual Studio Code is also open source on GitHub? Detecting a trend here?

To support cross-platform PowerShell script development, Microsoft started two new open-source projects. The PowerShell Editor Services project provides language and debug services for editors that want to support PowerShell script development beyond basic syntax colorization. In this regard, PowerShell Editor Services is similar to OmniSharp, which provides C# language support to editors like Atom, Sublime, Vim, and Visual Studio Code. The other project is vscode-powershell, which is a Visual Studio Code extension that integrates the PowerShell Editor Services. Both projects have taken significant community contributions.

Choose a PowerShell scripting environment

Before diving into the details about how to get started with Visual Studio Code, it is worth mentioning why you might want to use Visual Studio Code beyond the obvious need for Linux or macOS support.  Here are a few reasons why you might want to consider using Visual Studio Code:

  • Enhanced editor services
    • Open folder for editing (easily access multiple files)
    • Peek/Goto definition
    • Find all references
    • Search and replace across multiple files
    • Go to Symbol
    • Task runner (configure to run Pester tests)
    • Toggle render whitespace
    • Multiple cursor support (edit multiple lines at once)
    • Background script analysis by using PSScriptAnalyzer with code fix support coming soon
  • Enhanced debugging
    • Variables window with support for setting variables during debug
    • Watch window for watching variable values
    • Breakpoints window with support for line, function, and conditional breakpoints
    • Breakpoint hit count support
    • Call Stack window
  • Built-in support for Git version control
  • Support for multiple color schemes including dark and light schemes
  • Support for other file types: Batch, CSS, Dockerfile, Html, Ini, JavaScript, JSON, Markdown, Perl, Python, SQL, YAML, etc.
  • Rich extension ecosystem

On the other hand, if PowerShell ISE meets your needs, then by all means continue to use it. It’s not going anywhere. In fact, there are still a few things that PowerShell ISE can do that the PowerShell extension for Visual Studio Code can’t do – yet.  Here are the major features unique to PowerShell ISE:

  • Remote runspace tabs
  • Remote debugging
  • Language aware code folding
  • Interactive PowerShell console available during edit and debug

Install and configure Visual Studio Code

To install Visual Studio Code for your operating system, open http://code.visualstudio.com in a browser and follow the instructions. Even without the PowerShell extension installed, you will get syntax colorization and basic autocompletion for PowerShell scripts.

Syntax colorization is specified via a TextMate tmLanguage file. PowerShell syntax colorization is currently provided by the PowerShell Editor Syntax project on GitHub. If you find an issue with PowerShell syntax colorization, be sure to submit the issue to that project.

To get the full PowerShell script development experience, let’s install the PowerShell extension for Visual Studio Code. First, select Extensions from the View menu (or press Ctrl+Shift+X on Windows). In the Extensions view, type PowerShell in the search box, and then press Enter. The PowerShell extension from Microsoft should show up at the top of the search results.

Screenshot of PowerShell extension in search results

Click the Install button. After the PowerShell extension is installed, you will need to reload Visual Studio Code to use the extension. As an alternative, you can click the Reload button that appears where the Install button used to be.

On Windows, you are now set to develop PowerShell scripts in Visual Studio Code. However, on Linux and macOS, you will need to install PowerShell. You can find PowerShell releases for all platforms on GitHub and instructions to install PowerShell on these platforms.

At this point, you should be able to open PowerShell scripts or folders that contain multiple PowerShell scripts and edit them with rich IntelliSense support and enhanced editor features. You can also start with a new, empty file (called “Untitled-1”). You can tell PowerShell to treat that file as a PowerShell script by either saving the file with a .ps1 extension or by setting the language mode to PowerShell. To set the language mode, open the Command Palette from the View menu, or press Ctrl+Shift+P (Cmd+P on Mac).  Type change language mode or use fuzzy matching by typing clm,  andthen press Enter. After you’re in Select Language Mode, type ps to select PowerShell, and then press Enter.

Visual Studio Code basics

The basic layout of Visual Studio Code is shown in the following screenshot:

Basic layout of Visual Studio Code

The View Bar (A) lets you to quickly switch between the Explorer, Search, Git, Debug, and Extensions views.  The Side Bar (B) is collapsible (Ctrl+B / Cmd+B on macOS), so you can maximize the space that’s available for editing. Editor Groups (C) supports up to three side-by-side groups of editor windows. Editor Groups can be arranged both horizontally, as shown in the screenshot, and vertically. The Panel area (D) displays the Debug Console, Output, Problems, and Integrated Terminal. The Output window displays output from Extensions, Git, and Tasks. The Status Bar (E) displays useful information such as which Git branch is active, the repository status of the active branch, the number of problems and warnings, the cursor’s position, the current file’s encoding and line-endings, as well as the selected language mode for the current file.  You can even provide feedback to the Visual Studio Code team via the smiley face in the status bar. For a more in-depth getting started guide, see The Basics of Visual Studio Code.

Customize Visual Studio Code settings

One of the great features of Visual Studio Code is the extent to which you can customize it. Each extension usually provides customizable settings, too. To begin to customize Visual Studio Code, select the Command Palette from the View menu, or press Ctrl+Shift+P (Cmd + P on the Mac), type user, and then select Preferences: Open User Settings. This will open two editor windows as shown in the following screenshot:

Two editor windows in Visual Studio Code

As you can see in the Default Settings tab of the editor on the left, lots of areas of Visual Studio Code can be customized, including extensions like PowerShell. The editor on the left provides the “default” settings and is read-only. What you typically do is find a setting in this window that you want to customize, and copy it to the user settings file on the right. The screenshot also demonstrates the “Light+” color scheme.

Following is a basic set of customizations that I would recommend for PowerShell development:

// Place your settings in this file to overwrite the default settings {

"editor.rulers": [ 120 ],

"files.trimTrailingWhitespace": true,

"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"

}

The editor ruler provides a visual reminder of where you might want to wrap your PowerShell script. The trimTrailingWhitespace setting is particularly convenient for PowerShell. If you use ` for line continuation, you’ll never have to worry about inadvertent whitespace after a `. Finally, you will likely want Visual Studio Code’s Integrated Terminal to open PowerShell instead of CMD.exe.

The PowerShell extension comes with the following customizable settings:

Customizable settings for PowerShell extension

You can get more in-depth coverage of user settings in the User and Workspace Settings article in the Visual Studio Code docs.

Customize Visual Studio Code keyboard shortcuts

Visual Studio Code also enables you to customize keyboard shortcuts. To customize keyboard shortcuts, select Command Palette from the View menu or press Ctrl+Shift+P (Cmd + P on the Mac), and then type keyboard. Then, select Preferences: Open Keyboard Shortcuts.

Just like with user settings, Visual Studio Code will open two editor windows side-by-side. The editor window on the left, lists all the default keyboard shortcuts and is read-only. The editor window on the right is for your customized keyboard shortcuts.

Editor window for customized keyboard shortcuts

I generally don’t override many of the default keyboard shortcuts. However, as a long-time Windows user, I expect Ctrl+Shift+S to execute Save All instead of Save As…. You can change this keyboard shortcut like so:

// Place your key bindings in this file to overwrite the defaults [

{ "key":     "ctrl+shift+s",

"command": "workbench.action.files.saveAll" }

]

For more in-depth coverage of keyboard shortcuts, read the KeyBindings article in the Visual Studio Code docs. Also, check out these handy keyboard shortcut cheat sheets for Linux, macOS, and Windows.

In this blog post, we’ve gotten our feet wet with Visual Studio Code. You should have it installed, understand the basic layout of editors and tool windows, and know how to customize settings and keyboard shortcuts. In the next blog post, we will investigate settings that you can configure per workspace and the PowerShell language features provided by the PowerShell extension for Visual Studio Code.

Keith Hill Software Engineer PowerShell MVP

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.