Adding PowerShell Snippets to Visual Studio Code

So I’ve recently moved my daily work to a different laptop, a Yoga 900 with 16GB of RAM to be exact. I had been running Windows 8.1 but decided to jump in completely to a Windows 10 environment. As part of the process I’ve also made it a goal to begin using Visual Studio Code (VS Code) for my PowerShell work. Like many of you I am heavily invested in the PowerShell ISE so I know this won’t be easy. I’ve customized the PowerShell ISE extensively and have a lot of muscle memory that will need to be re-trained. One of the most important elements for me are snippets.

I rely heavily on intellisense and my own snippets to increase my efficiency when working on a script project. Once you get Visual Studio Code installed, along with the PowerShell extension, there is a lot going for you out of the box. Tab completion and intellisense seems to be even more pervasive than in the PowerShell ISE. I suspect many of the snippets I used to rely on are no longer necessary in VS Code.

But I still want to a few so here’s how.

All of the VS Code snippets are stored in language specific json files. In VS Code click on File – Preferences – User Snippets and start typing ‘PowerShell’.

image

Select PowerShell and hit Enter. This will open the default snippets file which is stored in $env:appdata\code\user\snippets\powershell.json.

image

What you see is a comment describing what to insert.  Move your cursor to above the closing curly brace and type the name for your snippet in quotes followed by a colon and open curly brace.

The snippet needs a prefix which will be the text you type to activate the snippet, a body and a description. End each line with a comma.

And end on the next line with a closing curly brace. The finished result should look like this:

image

Use Ctrl+S to save the file. Open a new file (Ctrl+N). By default this will be a plain text file.

image

But if you click on the type you can change it to PowerShell. Now VS Code will apply all PowerShell-related features including the new snippet. Start typing the prefix:

image

Hit Enter and the snippet is inserted.

image

Let’s add another snippet.  After the closing curly brace for the Quote snippet, type a comma and hit enter. Start the snippet on the next line.

I like using this line in my functions.

But if I stick that line into the snippet file you’ll see there is a problem.

image

Red text is never good and there is a squiggle under the opening [.  Json files can be tricky. The body value needs to be quoted as a string but I also need the verbose message to be inserted as a string with the double quotes so my variables expand. the trick is to escape those quote marks.

Save the PowerShell.json file, go back to the script and insert the ‘startingps’ snippet.

image

Close — but I didn’t get the $ in front the mycommand variable. In VS Code snippets you can insert placeholders like $1 and $2. In the current state VS Code thinks I’m trying to do that when I really need the literal $. After some trial and error this seems to work.

image

I inserted another $. Now when I save the file and insert the snippet it works as expected.

My snippets so far have been one-liners. But often I (and you ) will want to insert multiple lines. The only thing you need to do differently is insert the body as an object between a set of square brackets, and end each line with a comma.

If you are paying close attention you’ll also see “\t” inserted before Write-Verbose. This will insert a single tab.

image

If you need a blank line use  “\r”.

Sure, it may take a little time to get a snippet set up, but the point is that it is something you will use often and will pay for itself in no time.

As I continue to explore VS Code I’ll share my experiences either here on in the PowerShell section of Petri.com.

Leave a Reply

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