Working with the PowerShell ISE and Script Snippets

Posted on July 16, 2015 by Jeff Hicks in PowerShell with 0 Comments

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)

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)

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.

Viewing available snippets in GridView. (Image Credit: Jeff Hicks)

Viewing available snippets in GridView. (Image Credit: Jeff Hicks)

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-ISESnippet cmdlet lists the snippet files in the default location. (Image Credit: Jeff Hicks)

The Get-ISESnippet cmdlet lists the snippet files in the default location. (Image Credit: Jeff Hicks)

Sadly, the Get-ISESnippet cmdlet has no parameters. Because of this, you’ll need to take a few more steps if you’re searching for a specific snippet.

Searching for a snippet with the PowerShell Get-ISESnippet cmdlet. (Image Credit: Jeff Hicks)

Searching for a snippet with the PowerShell Get-ISESnippet cmdlet. (Image Credit: Jeff Hicks)

I decided to create my own version of Get-ISESnippet using my Copy-Command tool, which you can learn more about in my article, Making a PowerShell Command Your Own.

Sponsored

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)

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.

Get-MySnippet results. (Image Credit: Jeff Hicks)

Get-MySnippet results. (Image Credit: Jeff Hicks)

I can use this to find specific snippet files.

Finding specific snippets in Windows PowerShell. (Image Credit: Jeff Hicks)

Finding specific snippets in Windows PowerShell. (Image Credit: Jeff Hicks)

Or I can easily load snippet files for editing.

Sponsored

I hope my tools help you use snippets more in your own script development. Once you build up a library, you’ll be amazed at how quickly you can churn out a high-quality product.

Sponsored

Tagged with , , ,