Don’t Script: Create PowerShell Snippets

Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating custom Windows PowerShell snippets.

Hey, Scripting Guy! Question Hey, Scripting Guy! Thank you for yesterday's blog post, Don’t Write Scripts: Use Snippets. I like the idea of having access to Windows PowerShell script snippets. But I used to create my own snippets. It made it easier for me to work, and I could create a custom script based on my snippets in less than 30 seconds. I don’t see anything like that these days. Am I missing something? Please help me understand.


Hey, Scripting Guy! Answer Hello JT,

Microsoft Scripting Guy, Ed Wilson, is here. This morning, it is raining. It looks like spring time outside. It is almost warm, and it really is damp. So, as a result, I got soggy on my way to the gym for my morning workout. To me, it just seems wrong to drive to the gym, so I always walk or run there. Anyway, after drying off and changing, I got on the treadmill to finish warming up.

Luckily, the gym has WiFi, so I was able to check the email sent to when I finished my workout. JT, one of the cool things about Windows PowerShell ISE snippets, is that I can easily create my own snippets. I can even save them, and copy them to other devices. This makes them really great to use. One thing to keep in mind is that I cannot create a new snippet if the execution policy is set to AllSigned or Restricted. I will get an error message.

Anything that I type over and over again in my Windows PowerShell scripts becomes fair game for a script snippet. Of course, the things I have a tendency to type over and over again tend to depend on what sort of a script I am writing.

For example, if I am working with Word, I will obviously need to create an instance of the Word.Application object. If I am working on a script that connects to remote servers, I usually query Active Directory Domain Services to obtain a list of servers. So, I might need to create multiple snippets that cover the basic types of scripts I tend to write.

The cool thing is that there is a New-ISESnippet cmdlet I can use to create a new snippet. I can call the cmdlet directly, or I can write a script to create the snippet. I can even overwrite an existing snippet with the same name by using the –Force parameter. I specify the title, the description, author information, and the text. The text is the actual content of my snippet. The –CaretOffset parameter specifies how far over the insertion point moves. This parameter requires a bit of experimentation to get it right. Here is an example of a script that creates a new ISE snippet:

New-IseSnippet -Force -Title "Basic Heading" -Description "Basic script heading info" `

 -Author "ed wilson" -CaretOffset 18 -Text "

 # Script name:

 # Script description:

 # Script category:


 # Date:

 # Version: 1.0

 # "

When I run the script, the newly created snippet appears in the snippet list. (I use <Ctrl+J> to start the snippets, but I can also click Start Snippets from the Edit menu.) The newly created snippet is shown here in the drop-down list:

Image of menu

I select the snippet, press ENTER, and it is inserted into my new blank script file. This is shown here:

Image of command output

One thing to keep in mind is that even if I use the –Force parameter to overwrite existing snippets, if I run the script multiple times, I will get multiple instances of the snippet appearing in the snippet drop-down list. To fix this issue, all I need to do is to close the Windows PowerShell ISE and open it. The extraneous entries no longer appear in the drop-down list.

To delete a snippet, I use the Get-ISESnippet cmdlet and pipe the results to the Remote-Item cmdlet. This technique is shown here:

PS C:\> Get-IseSnippet

    Directory: C:\Users\ed\Documents\WindowsPowerShell\Snippets

Mode             LastWriteTime     Length    Name                                                       

—-                ————-                  ——    —-                                                       

-a—          3/3/2015  10:33 AM        783 Basic Heading.snippets.ps1xml                              

PS C:\> Get-IseSnippet | Remove-Item

JT, that is all there is to creating your own script snippets. Script Without Scripting Week will continue tomorrow when I will talk about saving scripts from applications.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (4)

  1. AllanM says:

    Thanks Ed, after yesterday’s blog on snippets I had already figured out how to create a new snippet with New-ISESnippet, then went searching to see exactly where it was and what was inside it.

    One correction you could make in the blog had me double checking:
    "To delete a snippet, I use the Get-ISESnippet cmdlet and pipe the results to the Remote-Item cmdlet. This technique is shown here:"

    should be Remove-Item, as you had in your example, not Remote-Item. Minor typo, but not everyone reads everything, including examples. 🙂

  2. Don Walker says:

    Thanks Ed! I will be using this feature quite a bit!

    I can imagine that when I create a series of snippets and want to remove one or two, I would definitely like to avoid deleting all of my Snippets with "Get-ISESnippet | Remove-Item".

    To remove specific snippets, I’m using the syntax (this is just an example to remove "custom Header" snippet):

    Get-ISESnippet | Where { $ -match ‘Custom Header’ } | Remove-Item -whatif

    I’ll then remove the ‘-whatif’ switch if I can confirm that the targeted name is the item(s) I want to remove. Otherwise, I’ll specify a more unique snippet name to narrow down my matches.

  3. Ross K says:

    Is there a way to add the date (get-date) to the script so that when you add the snippet to your script it will update automatically?

  4. RaphaelH says:

    There is a cool (free) tool to make the most out of snippets:

Skip to main content