Welcome to Day 8 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at //blog.waynesheffield.com/wayne/a-month-of-powershell/. Please refer to this page to see all of the posts in this series, and to quickly go to them.
What is a snippet?
In PowerShell 3 ISE, snippets have been added. If you’ve used Visual Studio, or one of the SSMS utilities that add in a snippet feature, you may already be familiar with snippets. So, what are snippets? In a nutshell, snippets are a method to allow you to paste text into the ISE console. In PowerShell, snippets are activated the same way that they are in Visual Studio… with CTRL+J. When you press that key combination, scroll box pops up with all of the snippets. If you hover over a snippet with your mouse, a description box also pops up to show the actual text that will be inserted.
There are three types of snippets:
- Default snippets (they ship with PowerShell ISE 3.0
- Module-based snippets
- User-defined snippets
The default snippets are the snippets that are shipped with PowerShell. If, for some reason, you don’t want the default snippets to appear in the snippet drop-down list, you can disable these in the ISE options, on the “General Settings” tab, the last checkbox.
You can import all of the module-based snippets for any module that has already been imported with the Import-Module cmdlet. These are performed with the Import-ISESnippet cmdlet, utilizing the Module parameter to specify the module.
User defined snippets allow us to create our own snippets that will be in the snippet drop-down list. They are created with the New-ISESnippet cmdlet. When created, an XML file named the title of the snippet is created in the $Home:\Documents\WindowsPowerShell\Snippets folder, and the file has an extension of “ps1xml”. As you might discern from the extension, this file is indeed an xml file. (You can also just build your own file and put it in this directory if you desire.)
Snippets in the home folder will automatically be loaded by the ISE whenever it starts. However, you can store your snippets anywhere that you desire – you will just have to load them yourself with the Import-ISESnippet cmdlet, specifying the path where they are located in the Path parameter to the cmdlet.
Okay, so let’s add a couple of snippets. First, on Day 4, in the Error Trapping section, we had a sample piece of code that can be used for generic error trapping. This seems like a good candidate to have as a snippet:
New-IseSnippet -Title "Error Trap-Generic" -Description "Generic Error Trapping routine" -Text ‘# Handle any errors that occur
# Handle the error
$err = $_.Exception
while( $err.InnerException )
$err = $err.InnerException
# End the script.
The next snippet that I’ll include will become useful in the near future when we start working with SMO – a generic header routine for the script files:
New-IseSnippet -Title "SMO Header" -Description "SMO Generic Header" -Text '
$Instance = "localhost\"
$DBName = ""
$SchemaName = ""
$ObjectName = ""
#Assign the SMO class to a variable
$SMO = "Microsoft.SqlServer.Management.Smo"
# get the server
$Server = New-Object ("$SMO.Server") "$Instance"
# assign the database name to a variable
$MyDB = $Server.Databases[$DBName]
# assign the schema to a variable
$Schema = $MyDB.Schemas[$SchemaName]
' -CaretOffset 150
The parameters for these are pretty simple – Title is the title of the snippet, as it shows up in the snippet drop-down list and the name of the ps1xml file. Description is just that – a description of the snippet. Text is the snippet itself. And in the second example, you can see that I used the CaretOffset parameter. This places the caret (aka cursor) at the specified position – in this case, 150 characters (just after the “\” in the definition of the $Instance variable).