Using Visual Studio Code for PowerShell Development

It has been a long time since the PowerShell integrated scripting environment (ISE) shipped with Windows PowerShell 2.0 in 2009. Since then .NET has become open source and cross-platform in form of .NET Core. Since PowerShell is built on top of .NET, it was an important pre-requisite before making PowerShell cross platform. Also, PowerShell has become cross-platform as of today. It is also open-source like .NET core. You can find the same at GitHub as well. In fact, we did discuss the open-source PowerShell namely 6.0 installation in few of the earlier posts as well

Perhaps, you are also aware that Visual Studio Code is open-source as well. It has become ongoing choice of platform development these days as its backed by PowerShell Team at Microsoft.  Going back at history, to support cross-platform PowerShell script development, Microsoft had 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. The other project is vscode-powershell, which is a Visual Studio Code extension that integrates the PowerShell Editor Services. Both projects have took significant community contributions.

Why Visual Studio Code for PowerShell development?

    • 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

It is also to be noted that it has become a great tool of choice by PowerShell team and they are putting a great deal of effort towards its development. So that reason alone is enough. However, there is nothing to be worry about if you want to stay with the ISE. It works great and is suitable for many cases. Also it’s available out-of-the-box or as part of windows features.

Install and Configure Visual Studio Code

To get started with Visual Studio Code, you need to install it on your choice of machine of development from https://code.visualstudio.com/. You can use either any operating system of your choice and follow instructions specific to that operating system installation and binaries.

Once it’s installed, open it and it will look like below:

visual-studio-code-first-view
Visual Studio Code – First View

To get started with PowerShell development, we’ll need to install the PowerShell extension. 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:

Select and install PowerShell Extension
Select and install PowerShell Extension

Click little green icon named Install associated with the extension. It will take few seconds and then get installed. After the PowerShell extension is installed, you will need to reload Visual Studio Code to use the extension. There should be a button named Reload, where the install button was:

Reload PowerShell Extension
Reload PowerShell Extension

On windows, this should be enough to get you started. However on Linux / MacOS, you would need to install the PowerShell. Check out this blog post on instructions for same.

Now, we can go ahead and select a folder to open existing PowerShell scripts. For this, open explorer (or Ctrl+Shift+E on windows) from View Menu:

Select a folder to open PowerShell scripts
Select a folder to open PowerShell scripts

Once you select a folder and a script, you should be able to see the scripting environment in its glory:

Viewing PowerShell scripts in Visual Studio Code
Viewing PowerShell scripts in Visual Studio Code

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,  and then press Enter. After you’re in Select Language Mode, type ps to select PowerShell, and then press Enter.

One thought on “Using Visual Studio Code for PowerShell Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s