I am ready to use Microsoft’s free editor Visual Studio Code (VSCode) for my PowerShell projects. What is your favorite editor?
Avatar
Latest posts by Timothy Warner (see all)

Which tool do you use to write, edit, and debug your Windows PowerShell scripts? It could be any of the following, or even another program:

Originally, I wasn't too impressed with Microsoft's Visual Studio Code—a free, open-source, cross-platform editor—when I used the initial versions. But then again, nobody aces their product on initial release!

As of this writing, we're up to version 1.1.0. In keeping with "cloud cadence" (DevOps-aligned release cycles), the VSCode dev team is committed to making monthly releases. Heck, you can clone the GitHub-based project and make contributions yourself if you're so inclined!

Honestly, I think I'm ready to ditch Sublime as my "go-to" text/script editor and devote myself to VSCode. Let's walk through how to use Visual Studio Code as a Windows PowerShell script editor.

Please read my earlier 4sysops introduction to Visual Studio Code if you're in need of more general information.

Install the PowerShell extension

PowerShell Editor Services is an application programming interface (API) whose goal is to make the PowerShell environment portable across different host applications, even those on non-Windows platforms.

As a side note, PowerShell MVP and expert developer Adam Driscoll is hard at work enabling remote PowerShell debugging from Linux or OS X systems; keep your eyes peeled for that feature!

Start VSCode and click View > Command Palette (or press CTRL+SHIFT+P) to open the Command Palette. If you've used Sublime Text, then this extension paradigm will instantly be familiar. In point of fact, VSCode "borrows" an astounding number of features from Sublime Text.

Anyway, type ext install powershell, arrow down to the PowerShell entry, and press ENTER to install the PowerShell Language Support for Visual Studio Code extension as shown in the next screen capture.

Installing the VSCode PowerShell extension

Installing the VSCode PowerShell extension

The VSCode PowerShell extension incorporates PowerShell Editor Services’ functionality. Some of these features include:

  • Code snippets
  • Syntax highlighting
  • Intellisense code completion
  • PSScriptAnalyzer-based script analysis
  • Local debugging in a basic interactive console

Restart the editor and we're ready to rock!

Browse the sample folder

The PowerShell extension includes a pile of scripts intended to help you overcome your VSCode script editing learning curve faster. In VSCode, click File > Open Folder and browse to the following path:

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

Go ahead and open Stop-Process2.ps1. This simple script just kills any process you pass into the function via its mandatory -Name parameter. Take a look at the following annotated screenshot and I'll briefly get you comfortable with the VSCode interface:

The VSCode user interface

The VSCode user interface

  • A: Explorer. View individual files or entire directory contents
  • B: Search. Scan all documents in your current workspace by using traditional matching or regular expressions
  • C: Git. After you install Git on your workstation, you can initialize a repository and track your changes directly from VSCode
  • D: Debug. Perform live debugging that includes the standard features: breakpoints, variables, watch and call stack
  • E: Extensions. Provides a shortcut method to the Command Palette and allows you to manage your extensions
  • F: Errors and Warnings. Quickly highlight potential PowerShell code issues
  • G: Split Editor. VSCode allows you to tile up to three separate documents in the same application instance, making code review and versioning easier
  • H: Language Mode. Switch from PowerShell to one of the other supported scripting/programming languages

Those four buttons (Explorer, Search, Git, and Debug) are toggles; repeatedly click each to switch between that particular view and hiding the tool pane entirely. Pretty neat!

Set your preferences

Once again, Visual Studio Code takes a leaf from Sublime Text in how it handles user (or workspace) preferences. Click File > Preferences > User Settings to open your settings.json JavaScript Object Notation (JSON) preferences file.

You set preferences by (a) finding the appropriate preference key-value pair in the Default Settings file, which appears to the left of your settings file; (b) copying and pasting that data into your user preferences file; and (c) editing the value as appropriate.

For example, I want to change the default font family and font size. Here's my updated JSON, which you can also see in the following screen capture:

    // Controls the font family.
    "editor.fontFamily": "Consolas",

    // Controls the font size.
    "editor.fontSize": 14,

This Stack Overflow thread contains some interesting discussion regarding how Visual Studio selects a default font/font size depending on your local machine's environment.

Setting user preferences in VSCode

Setting user preferences in VSCode

Start coding!

Go ahead and start a new script file and save it as get-uptime.ps1. As you type the following simple function that calculates the local system's uptime, I want you to pay attention to the following:

  • TAB completion for commands and parameters
  • IntelliSense drop-downs for parameters, enumerations, properties, and methods
function Get-Uptime {
   $os = Get-WmiObject -Class Win32_OperatingSystem
   $uptime = (Get-Date) - `
      ($os.ConvertToDateTime($os.LastBootupTime))
   $Display = "Uptime: "+ $uptime.Days + " days, " + $uptime.Hours + " hours, " + $uptime.Minutes + " minutes"
   Write-Output $Display
}
Get-Uptime

I show you these integrated development environment (IDE) features in the next screenshot:

Any PowerShell editor worth its salt needs IntelliSense.

Variable management is super easy in VSCode. For instance, right-click a variable and select one of the options from the shortcut menu:

  • Go to Definition: Places the cursor where you defined that variable
  • Peek Definition: Puts the variable definition in a small sub-pane
  • Find all References: Easily locate every reference to that variable in your script(s)
  • Change all Occurrences: Convenient method to do a global find/replace on variable names

Click the Bug icon to navigate to the built-in debugger. The live Debug Console isn't as user-friendly as I'd like, but I guarantee we'll see its usability increase as we march forward in time and product release cycles.

Use the Debug toolbar to start, pause, restart, or stop a debugging session. To run my Get-Uptime script, I typed .\Get-Uptime into the Debug Console and pressed ENTER. For some reason, even though I called the function in the script, simply starting the debugger didn't execute that call.

Click in the gutter to set a breakpoint, and restart the debugging session. As you can see below, you can view variable contents and function calls on the stack.

VSCode includes a capable debugger

VSCode includes a capable debugger.

Closing thoughts

If you think that Visual Studio Code will give you the same Read-Evaluate-Print-Loop (REPL) environment that you have now in the ISE, then you'll be disappointed. Your best bet is to run VSCode and an administrative PowerShell console session alongside each other and simply dot-source your VSCode-edited scripts into the console for execution.

Subscribe to 4sysops newsletter!

On the other hand, VSCode and the PowerShell extension are updated literally every day at GitHub, so we can expect to see this tool's capabilities and performance improve proportionally over the coming days, weeks, months, and years.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account