Creating Snippets in PowerShell

For those of you using PowerShell V3’s new Integrated Script Editor (ISE) and be honest, why aren’t you? Besides all of the awesome stuff with code folding, intellisense, etc…, there is also another cool feature that is available is the ability to create and use snippets in the ISE.

An example is the already available snippets that are available in the ISE. All you have to do is click CTRL+J (you can also right click and select Start Snippets if that is your thing) and the snippets context menu opens up with a nice selection of snippets to choose from.

image

You can scroll through all of the available snippets and each one has a tooltip which allows you to see the code as well as a description/author of the snippet. Once you select the snippet, it will be pasted into the current tab on the ISE session. Given, all of these snippets are just templates that you can use to more easily create an advanced function, new workflow or just add some flow control to an existing script or function.

So what if you have your own set of snippets in a file or something that you wanted to create on the fly to have available later? Luckily, the PowerShell team put together a small module called ISE in PowerShell V3 that makes it very easy to create and add your own snippets into the ISE which are just PS1XML files. You can even use (to an extent) the module in a PowerShell console.

The three cmdlets available in the ISE module are:

  • Get-IseSnippet
  • Import-IseSnippet
  • New-IseSnippet

Scripts created by users are created and saved in your $home\WindowsPowerShell\Snippets folder. Because I do not have a “user made snippet”, when I run Get-IseSnippet, nothing happens.

Get-IseSnippet

image

Creating Your First Snippet

Using the ISE, I type the following code to run that will create the snippet and store it in the Snippets folder mentioned earlier.

New-IseSnippet -Title "Test Snippet" -Author "Boe Prox" -Description "Just a test snippet" -Text "
Function Verb-Noun {
    [cmdletbinding()]
    Param (
        [parameter()]
        [string[]]$Object
    )
    Begin {}
    Process {}
    End {}
}
"

Now lets run the Get-IseSnippet again and see what happens.

image

And there is the new snippet ready to go. Because I did this in the ISE, it was already loaded into the current session and available to use!

image

The mandatory parameters to use when creating a snippet are the Description, Title and Text with the Text being the actual code snippet. This must be a string and not an array as it will throw an error if using anything other than a string.

Remember when I said you could create snippet from the console? Well, the only catch to this is that because you are not running it from the ISE, it will throw an error stating that it couldn’t load the snippet into the ISE (because it doesn’t exist in a console, of course!). The file is still created and will be loaded into the ISE when you start it up or if you use the Import-IseSnippet cmdlet.

image

By the way, it is just a file, so you can use Remove-Item to easily remove that snippet file. Or if you wanted to overwrite it, simply use the –Force parameter as show in my example.

If you have a file that you want to make a snippet, it is simple to make it into a snippet.

New-IseSnippet -Title "Test Snippet" -Author "Boe Prox" `
-Description "Just a test snippet" `
-Text (Get-Content .\SomeCodeSnippet.ps1)

This should work, right? Nope! This will fail because when you use Get-Content like I shown, it is actually will not work because the data returned is an object, not a string that it requires.

image

get-member -input (Get-Content .\SomeCodeSnippet.ps1)

image

You will need to use the –Raw parameter in Get-Content to make it work correctly.

get-member -input (Get-Content .\SomeCodeSnippet.ps1 -Raw)

image

This is the correct code to use to create a new snippet from a file.

New-IseSnippet -Title "Test Snippet" -Author "Boe Prox" `
-Description "Just a test snippet" `
-Text (Get-Content .\SomeCodeSnippet.ps1 -Raw)

You could also pipe the the output of Get-Content to Out-String as well to get the same results.

Lastly, let’s assume that there is a repository of snippets out on some server that you want to be able to add into your ISE snippets library. To do this, we would use Import-IseSnippet.

Import-IseSnippet -Path "\\dc1\SharedStorage\snippets" -Recurse

I just decided to grab all of the snippets that were in that folder to import into my current ISE session. Because I imported the snippets, they are not added to my personal snippets folder and once you close out of the ISE session, the snippets must be re-imported back into the ISE session.

Hopefully this will get you started to create your own PowerShell snippets using what I have already shown you! I would love to see what snippets you have created and what you are using them for. Available for download below is a snippet I created that I use frequently for creating background runspaces on various types of queries I use for functions. Feel free to download it and let me know your thoughts!

PowerShell Runspace Snippet

image

This entry was posted in powershell, V3 and tagged , , . Bookmark the permalink.

4 Responses to Creating Snippets in PowerShell

  1. Pingback: Quickly Create a PowerShell Snippet in the ISE Using an Add On | Learn Powershell | Achieve More

  2. Pingback: Tipy pro Powershell – 2. díl | Igorovo

  3. Pingback: Tipy pro Powershell – 2. díl | Igorovo

Leave a reply to oleg40a Cancel reply