Working with the PowerShell ISE and Script Snippets

I don’t like typing PowerShell scripts any more than the rest of you, so I rely extensively on snippets to save time. Microsoft includes a fair number of snippets out of the box, where you can easily access them in the PowerShell ISE by pressing Ctrl+J to display a popup list. Scroll to the one you want and press enter. In this article, I’ll teach you how to see and use different snippets that you already have available in PowerShell.

In the PowerShell ISE, you can use the PSISE object model to access the current PowerShell tab. Each tab can have its own set of snippets.

Each PowerShell tab has its own snippets. (Image Credit: Jeff Hicks)

You can type $psise.currentpowershelltab.snippets to see all of them. You’ll see something like this for each snippet:

Viewing snippet information in PowerShell. (Image Credit: Jeff Hicks)

If you were curious about what you have available, then you can use a command like this to display relevant information.

Because I’ve already added my own snippets, my results will vary from yours.

Although it’s fun playing with the PSISE object model, there’s also a cmdlet called Get-ISESnippet. Whereas the PSISE object retrieves snippet information by content, this cmdlet lists the snippet files in the default location.

The Get-MySnippet cmdlet gets the PS1XML files that contain reusable text "snippets" that the user created. It works only in Windows PowerShell ISE.

When you use the New-IseSnippet cmdlet to create a snippet, New-IseSnippet creates a <SnippetTitle>.Snippets.ps1xml file in the $home\Documents\WindowsPowerShell\Snippets directory. Get-MySnippet gets the snippet files in the Snippets directory.

Get-MySnippet does not get built-in snippets or snippets that are imported from modules by using the Import-IseSnippet cmdlet.

This cmdlet is introduced in Windows PowerShell 3.0. This specific command is a modified version of Get-ISESnippet.

.PARAMETER Name

The name of a snippet to get. Wildcards are allowed. The default is all snippets.

.EXAMPLE

PS C:\>Get-MySnippet

This command gets all user-defined snippets in the Snippets directory.

Get all snippets that start with 'Validate' and send the results to Out-Gridview. Select snippets will be opened in the ISE for editing.

.NOTES

The New-IseSnippet cmdlet stores new user-created snippets in unsigned .ps1xml files. As such, Windows PowerShell cannot add them to a session in which the execution policy is AllSigned or Restricted. In a Restricted or AllSigned session, you can create, get, and import unsigned user-created snippets, but you cannot use them in the session.

To use unsigned user-created snippets that the Get-MySnippet cmdlet returns, change the execution policy, and then restart Windows PowerShell ISE.

For more information about Windows PowerShell execution policies, see about_Execution_Policies.

In my version, I included a parameter to filter by file name including the use of wildcards.

Filtering by name with wildcards. (Image Credit: Jeff Hicks)

This output is the same you would get with Get-ISESnippet, but I also added a parameter to create more detailed output. I wanted to see some of the same information available when using the PSISE object.

Because the snippet files are XML, I can use Select-XML with an XPath filter for the different attributes I want. The tricky part in this situation is that the query requires the XML namespace.

The XPath query will return node objects. The InnerText property should contain the values I want. I should point out that it is technically possible to have multiple snippets in a single snippet file. My technique will most likely fail in that scenario. If you stick to one snippet per file, then this won’t be an issue. After I get all of the details, I write a custom object to the pipeline for each snippet file.

MEMBER LOGIN:

BECOME A PETRI MEMBER:

About the Contributor

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, teacher and consultant. Jeff has written for numerous online sites and print publications and is a frequent speaker at technology conferences and user groups. His latest book is PowerShell Scripting and Toolmaking.