Creating PowerShell ISE v3 (and later) Code Snippets

When using the PowerShell ISE , similar to other scripting editors, you have access to what are known as ‘code snippets’. These are quick start ways to generate frequently used code, for instance if there is something you use regularly and can never remember the syntax for, or maybe it is too long to be practical to remember it.

PowerShell ships with some default snippets and it is also possible to add some custom snippets of your own. They can be accessed via the keyboard shortcut Ctrl-J – the below screenshot displays the default snippets available.

ISESnippet01

Selecting one of these snippets will populate the editor with the sample code. In the example below I picked the switch snippet, not one that’s always easy to remember, and an example is placed in the editor which I can modify for my needs.

ISESnippet02

Naturally, you may want to create some of your own, known as User-Defined Snippets. PowerShell v3 ships with some cmdlets to help you create and manage these, starting with the New-ISESnippet cmdlet. Give the code you wish to use for your snippet to this cmdlet and it will create a <Title>.Snippets.ps1xml file in the $home\Documents\WindowsPowerShell\Snippets directory with the title that you specify .

For example, one thing that students in the PowerShell classes I teach often struggle to remember is the syntax for creating a calculated property with Select-Object, like say this:

Get-ChildItem C:\Test | Select-Object Name, CreationTime,  @{N="Kbytes";E={$_.Length / 1Kb}}

So, we can create a snippet to help with this via the following (Note, the CaretOffset parameter places the cursor in a specific place after using the snippet, in this case at the start of the word ‘Title’ that you need to edit. Aren’t I helpful?!):


New-IseSnippet -Title "Calculated Property" -Description "Syntax for a Calculated Property" -Text "@{N='Title';E={ }}" -CaretOffset 5

This will create the below XML file in my  snippets folder for both current and future use every time I open the PowerShell ISE.

ISESnippet03

Now if I get to a point where I can’t remember the syntax for a calculated property….

ISESnippet04

I can select the snippet and have it populate that difficult to remember syntax for me:

ISESnippet05

ISESnippet06

So that is cool for short syntax items, but what about something which is multiline, how would I create a snippet for that? One answer is to use a Here-String.

For example, say you wish to add a parameter block like this as a snippet


Param
 (

[parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [ValidateNotNullOrEmpty()]
 [String[]]$Parameter1,

 [parameter(Mandatory=$false,ValueFromPipeline=$false)]
 [ValidateNotNullOrEmpty()]
 [PSObject[]]$Parameter2
 )

First of all store this text in a Here-String then use that Here-String with New-ISESnippet . Tip: ensure you use single quotes rather than double to create the Here-String otherwise the variables will be expanded to blank, since they (probably) don’t exist.


$Snippet = @'
 Param
 (

[parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [ValidateNotNullOrEmpty()]
 [String[]]$Parameter1,


 [parameter(Mandatory=$false,ValueFromPipeline=$false)]
 [ValidateNotNullOrEmpty()]
 [PSObject[]]$Parameter2
 )
'@

New-IseSnippet -Title "Param Block" -Description "Syntax for a Param Block" -Text $Snippet

Now I can create a Param Block with this multiline snippet

ISESnippet07

ISESnippet08

and our snippets folder contains two XML files, one for each snippet:

ISESnippet09

Having multiple snippet files may or my not be a good thing, depending on your scenario. It’s possible if you want to share these that you may want to combine them into a single file. This post on the PowerShell team blog details an XML snippet example and its structure. If we take the content between <Snippet Version=’1.0.0′> and </Snippet> we can combine each snippet into one file which is maybe easier for distribution to others if you want them to have all of those snippets.

ISESnippet10

Next week I’ll be posting some snippets that are part of my UKVMUG session which you may find useful, all combined into one file…….

2 thoughts on “Creating PowerShell ISE v3 (and later) Code Snippets

Leave a Reply

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