Summary: Sean Kearney shows you how to get sample code in the Windows PowerShell ISE.
Honorary Scripting Guy, Sean Kearney, is here today to show you a really cool feature that has been in Windows PowerShell ISE for a while, but you might have overlooked it! It’s called Snippets.
Note This is Part 4 in a five-part series. Also see:
I love using the Snippets feature on a regular basis, because although I can work in PowerShell, I don’t memorize code. I’m an IT pro, not a dev. Even my good developer friends don’t memorize everything.
The Snippets feature was actually introduced in Windows PowerShell ISE 3.0, and I’ve always found it to be an invaluable tool.
Press CTRL+J on the keyboard to pull up the main list of Snippets.
As you can see, there is a large pile of prebuilt code examples in the PowerShell ISE. If you’d like to use one, simply select it from the list. You can use the arrow keys to navigate up and down the list, or you can use your mouse.
Clicking an example immediately pastes it into the active window, whether that is the Script Panel or the Console view in the ISE. Here is an example of the do-while loop in the Snippet list and pasted into the editing window:
The cool part about the Snippets is that although Microsoft provides a strong rudimentary list, you can also add to it! Adding your custom snippets requires an example stored as a here-string and the New-ISESnippet cmdlet.
In the following example, I am going to add a sample to remind myself how to define a string array, including some comments I should remember to edit. Here is the original code in PowerShell:
# My Example String
#
# Some string array with a description
[string[]]$SampleValue=@()
…and here is what was required to add it as a personal Snippet in my PowerShell ISE.
$Text=@"
# My Example String
#
# Some string array with a description
[string[]]$SampleValue=@()
"@
$Title=”Array Sample”
$Description=”How to create a simple String Array including Comments”
$Author=”Sean Kearney, Honorary Scripting Guy”
New-ISESnippet –Text $Text –Title $Title –Description $Description –Author $Autho
If I execute this code and press CTRL+J immediately to see the list of Snippets, I can see that this new Snippet was added:
You’ll also notice that the new Snippet is an XML file within the user’s personal PowerShell folder under a special folder called Snippets. A list of all personal Snippets for a user are within this folder.
If you back up this folder, you can easily transfer any new Snippet examples between computers. As you reload your ISE each time, the new Snippets are immediately available to you.
Of course, if you’ve decided you don’t like a particular example, you can easily remove it from the system by deleting its XML file or moving it out of the PowerShell Snippets folder.
You can also (if you feel so inclined) edit the sample Snippet in the XML file. Here is the Snippet I just created. You can see the sample code within the block marked <Code> and </Code
You can simple edit this file within any standard text editor, such as Notepad.
Sure, Snippets is an older feature, but it’s one of those nice things that makes the built-in ISE nice to use for an IT professional.
Tomorrow, I’ll wrap up this week about features I love to use in the Windows PowerShell ISE. I’ll be talking about remote text file editing with the PSEdit cmdlet.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.
Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP