With their recent release of Visual Studio Code, it seems to me that Microsoft is clearly looking to put the Sublime Text editor out of business.

In a nutshell, Visual Studio Code (VSC) is a free, multi-platform, multi-language, extensible, and open-source code editor. Specifically, VSC:

Linux and OS X users can use Visual Studio Code to edit, test, and debug any Web development project, including Node.js and even ASP.NET v5-based projects. To prove this, look at the following screenshot that shows me using VSC on my Ubuntu workstation:

Visual Studio Code runs on Windows, OS X, and Linux

Visual Studio Code runs on Windows, OS X, and Linux.

For our purposes today, however, we're concerned with using VSC as a Window PowerShell code editor. Let's do this!

Installing VSC and the PowerShell extensions ^

You can download and install Visual Studio Code from the project Web site. You can review and fork the VSC project source code by visiting GitHub.

As of this writing, I don't see VSC listed at the PowerShell or Chocolatey galleries; I would imagine that will change in time.

VSC includes built-in support for many, many programming and scripting languages. For instance, you can open the Select Language Mode menu, choose C#, and BANG! Now you have IntelliSense code completion and contextual help tailored to your preferred development language!

Visual Studio Code supports all the most popular programming and scripting languages.

Visual Studio Code supports all the most popular programming and scripting languages.

One important caveat for us PowerShell scripters is that the VSC PowerShell extension requires PowerShell v5. This means that either your administrative workstation must run Windows 10, or you need to install Windows Management Framework (WMF) v5 Production Preview or later.

Here's your first Visual Studio Code lesson: Use F1 to open the Command palette. The Command palette should be instantly recognizable if you've used Sublime Text for any length of time. Alternatively, you can press CTRL+SHIFT+P or click View > Command Palette to do the same thing.

You'll be using the VSC command palette all the time in your work

You'll be using the VSC command palette all the time in your work.

Trust me—you'll be using the Command pallete a lot in VSC, so get used to using the keyboard shortcuts. To find the PowerShell extensions, type extension into the Command palette, select Extensions: Install Extension from the list, and then press ENTER.

After a moment's pause, you'll see an alphabetized list of VSC extensions. Type PowerShell, and you'll see results that look similar to mine:

VSC has some excellent Windows PowerShell-related extensions

VSC has some excellent Windows PowerShell-related extensions.

We're interested in the official PowerShell language extension authored by Microsoft. Click its Command palette menu entry to install, after which you'll be prompted to restart VSC. Once you do so, then we're almost ready to start editing some PowerShell code!

Script editing and debugging ^

Visual Studio Code employs a folder-based view that allows us to manage multiple files simultaneously. In VSC, click File > Open Folder and add the following folder to your project:

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

This is actually pretty cool—the PowerShell team gave us some handy documentation and code examples!

Make sure to open the PowerShell extension example files in VSC

Make sure to open the PowerShell extension example files in VSC.

Next, click to open DebugTest.ps1. Do you see the red squigglies? Hover your mouse over them, and you'll see real-time code improvement suggestions, courtesy of the built-in PowerShell Script Analyzer.

Visual Studio Code has many IDE features built in

Visual Studio Code has many IDE features built in.

Now move your cursor to the end of the file, and type the following command, which uses a pipeline to test out the PowerShell extension's Intellisense features:

Get-Service | Where-Object -FilterScript {$_.Status -eq 'Stopped'}

Here's your second Visual Studio Code lesson: Press F8 to run your selection using VSC's built-in PowerShell parser. Alternatively, you can open the Command palette, type Run and select PowerShell: Run Selection. Unfortunately, you actually need to highlight your selection rather than simply place your cursor within it.

Let's quickly test script debugging. Delete the Get-Service pipeline we added to DebutTest.ps1 and press CTRL+SHIFT+D to open the debug workspace. Press F5 to instruct the debugger to run the current script.

It's amazing how much script debugging capability Microsoft added to this free tool! For instance, you can:

  • Add variables to the Watch pane to observe their values over time
  • Set breakpoints to troubleshoot particular sections of a script
  • Observe the call stack to analyze exactly how PowerShell parses your script

Visual Studio Code has a robust debugging interface

Visual Studio Code has a robust debugging interface.

Integrated source code control ^

VSC integrates with Git source control. Click the Git button in the navigation bar. If you have Git installed, you can immediately initialize a local Git repository for the current project folder. If you don't have Git, then you'll be prompted to download it.

VSC includes integrated Git source code control

VSC includes integrated Git source code control.

Wrapping up ^

As much as I enjoy Visual Studio Code, for now I'm sticking with Sublime Text and the Windows PowerShell ISE (with ISESteroids, of course). Here are my reasons:

  • I've had several VSC app crashes when performing relatively simple tasks (like running simple PowerShell pipelines with F8).
  • The Intellisense code completion is quite slow.
  • Many features and keyboard shortcuts aren't obvious and/or visible in the user interface.

Given time and continued community participation, I have no doubt that Visual Studio Code will be a "must have" utility for Windows PowerShell scripters and DevOps professionals. Kudos to Microsoft for embracing open-source technologies and non-Microsoft participation in their products.

+1
0 Comments

Leave a reply

Please enclose code in pre tags

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

*

© 4sysops 2006 - 2021

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