Does this sound familiar? Have you ever…
Opened up old scripts in order to remember exactly how you used a particular cmdlet?
Saved script templates so that you’d have an example of a particular cmdlet or syntax?
Ripped out your hair in frustration because you can’t find those old files or templates?!
Awesome. You’re human. We’ve all done that. Especially us forgetful folks.
Now, wouldn’t it be fabulous if there was a much simpler way to save your awesome syntax and your hair? Well, you’ve come to the right place!
PowerShell ISE snippets
The PowerShell ISE has a slick little feature called snippets that you may or may not be familiar with! They were introduced way back in version 3 of the ISE.
You may already be using this feature without realizing it. Have you ever pressed Ctrl+J while using the ISE? Maybe you’ve right-clicked and opened up the context menu and selected start Snippets. Both of these pop up the available snippets!
Go on! Try it out! All you need to do is select a snippet from what’s available.
At this point, you may be thinking, “That’s pretty nifty, but what good does that do me?”. Lots of good, I say. Lots!
You will find that there are many default snippets that are available for you to use right out of the box. How thoughtful of the PowerShell team!
Need a Do While snippet? Done.
Need a Try/Catch snippet? Piece of Cake.
Need DSC template snippets? Fuhgeddaboudit!
Now, this is all fine and dandy, but what happens when you want something that’s simply not available?
You pick up your mighty PowerShell hammer and create it!
New-IseSnippet
Brace yourself. You’re going to love this. There is a cmdlet made specifically for creating PowerShell ISE snippets. *gasp!*
It’s called New-IseSnippet and it is wonderfully wonderful.
New-IseSnippet only has a few parameters, so it is really easy to get a new snippet created. Here is a list of those parameters.
Title string (required): This is what shows up in the snippets popup. It’s also the name of the snippet file.
Description string (required): This is what shows up in the description section of the snippets popup.
Text string (required): This is the PowerShell code that is actually added to your editor.
Author string: This shows up in the snippet file itself rather than within the editor.
CaretOffset Int32: This is the character offset for the position of the cursor after adding the snippet to your editor.
Here is where some of the parameters will show up within the snippet.
Now, enough with the talk. Less talk and more typing!
Creating PowerShell ISE snippets with New-IseSnippet
Hands down, the hardest part of this entire process is coming up with a snippet that you want to create.
To start, let’s keep it simple and find something that you often use in your code. I often find myself using Test-Path in an If statement, so here’s an example snippet that I might create:
If (Test-Path $SomePath) {
# If Path valid
} Else {
# If Path not valid
}
Give that a spin. When you open up the snippets popup menu, you’ll see “If Test-Path” is an available snippet to use.
Now, when you select it, you should magically see
If Test-Path appear.
Easy peasy. You’ve got this down. You’re pretty much a pro at this now.
So, let’s add another luscious layer to this delectable cake.
Creating PowerShell ISE snippets with a PowerShell ISE snippet
While on the subject of PowerShell ISE snippets, I decided to create a snippet that creates snippets, a little “snipception” if you will.
Mostly, I wanted to give an example of using the -CaretOffset parameter and thought that this would be a fun example.
$Title = ""
$Description = ""
$Author = ""
$Text = @"
"@
New-IseSnippet -Title $Title -Description $Description -Author $Author -Text $Text
This will create a snippet called Snippet Template that you can use for creating even more snippets!
The -CaretOffset 16 part means that the cursor will start after the 16th character when the snippet is used.
Wrapping up
Well, that’s basically it. As you saw, it is fantastically fabulous. So, if you use the ISE, you should take a look at creating your own custom snippets.
Bonus
Just in case you were wondering (and I’m sure you were), your newly created snippets will be stored in your user profile directory:$Env:USERPROFILE\Documents\WindowsPowerShell\Snippets
These are saved as XML files (.ps1xml files). That means that you can use all the powers of XML manipulation to create, modify, save, backup, and even cherish those files. Hurray!