Automating the world one-liner at a time…
This blog post applies to the Windows PowerShell Integrated Scripting Environment (ISE) in Windows 8 and Windows Server 2012. The ISE is also fully supported and available when Server 2012 is deployed using the Minimal Server Interface. To start ISE, type ise in a PowerShell console and press Enter
Snippets provide a convenient way to paste arbitrary text into the ISE console and script editor, right where you need it. The idea is to provide improved productivity, discoverability, and usability:
For those of you who are familiar with Visual Studio’s Snippets, the ISE version uses the same shortcut key, but it will surely feel like a “poor man’s version” :-), lacking the ability to tab between fields, among other things.
You can list available Snippets by pressing Ctrl+J or selecting “Start Snippets” from the “Edit” menu. Notice that a tooltip displays the actual text which will be inserted, as you move over the selections in the Snippets drop-down. Here’s what it looks like:
There are 3 types of snippets:
Consider these to be the “starter set” of snippets: PowerShell code that is either frequently used, or hard to discover. Press Ctrl+J to start Snippets and look through the various default snippets.
For those of who may have developed a specific coding style over the years, and feel very strongly about whether the curly braces belong on the same line or on the next line :-), you’ll be pleased to know that you can hide the default snippets, either through the Options dialog (at the bottom of the “General Settings” tab), or directly through the ISE object model at the command line:
You can create your own snippets and add them to the snippets drop-down list using the New-IseSnippet cmdlet. Here’s the cmdlet syntax, using one of my favorite new features: Show-Command:
Note that you can also type New-IseSnippet anywhere in the ISE, and then press Ctrl+F1 when the caret (you may be calling it “cursor”) is anywhere within the cmdlet string.
Here’s an example: I often find myself wanting to show the definition (body) of a function. for that, I would have to type Get-Command, followed by the function name, wrap the whole thing in parentheses, and then append .Definition. A snippet comes in handy:
New-IseSnippet -Title "Show Definition" -Description "Shows command definition" -Text "(Get-Command ).Definition" -CaretOffset 13
This example adds a “Show Definition” snippet to the snippets list, and inserts the following text when selected: (Get-Command ).Definition
If you prefer to hand-craft your snippets instead of using the cmdlet, just create an XML file like the one below, and copy it to the snippets “home” folder, which you can get using one of the following methods:
Any snippets in the snippets home folder will be loaded automatically by the ISE when it starts. If you don’t want to place your snippets in the home folder, you can place them anywhere (C:\temp for example), but then you must load them explicitly in your ISE profile using: Import-IseSnippet -Path 'C:\Temp'
Snippets files have an extension of .snippets.ps1xml and have the following structure:
<?xml version='1.0' encoding='utf-8' ?> <Snippets xmlns='http://schemas.microsoft.com/PowerShell/Snippets'> <Snippet Version='1.0.0'> <Header> <Title>Show Definition</Title> <Description>Shows command definition</Description> <Author></Author> < SnippetTypes> < SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Code> <Script Language='PowerShell' CaretOffset='13'> <![CDATA[(Get-Command ).Definition]]> </Script> </Code> </Snippet> < /Snippets>
<?xml version='1.0' encoding='utf-8' ?> <Snippets xmlns='http://schemas.microsoft.com/PowerShell/Snippets'> <Snippet Version='1.0.0'> <Header> <Title>Show Definition</Title> <Description>Shows command definition</Description> <Author></Author> < SnippetTypes> < SnippetType>Expansion</SnippetType> </SnippetTypes> </Header>
<Code> <Script Language='PowerShell' CaretOffset='13'> <![CDATA[(Get-Command ).Definition]]> </Script> </Code>
</Snippet> < /Snippets>
To load all module-based snippets (which only works for modules which have already been imported using Import-Module), use: Import-IseSnippet -Module Xyz
If you’re shipping a module with snippets, place your snippet files into a “Snippets” folder, directly under the module folder.
I hope that this quick walk-through will encourage you to use snippets, create your own, and share them with the community.
Program Manager, Windows Server Manageability
You won't believe this, but I've been looking for the last 20 minutes or so for where snippets were stored. Thanks for the info!
What about multiline custom snippets?
Chad, use here-strings:
$code = @'
$objUser = New-Object System.Security.Principal.NTAccount("$strDomain", "$strDomainUser")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
New-IseSnippet -Title "Get-SIDfromDomainUser" -Description "Retrieve SID from a Domain User in a specified Domain" -Text $code
For the Dutch reader who can't find ISE in Windows 8 this article should help you: blog.master-it.nl/.../Waar_is_ISE_voor_Powershell_3_gebleven_in_Windows_8.htm
I'm using a few lines of code (actually a snippet) to help me create new snippets with multiple lines of code.
Check my solution at powershelleverydayfaq.blogspot.ca/.../how-to-create-new-snippet-in-powershell.html
I've made a (free) code snippet manager for ISE: bytecookie.wordpress.com/snippet-manager
Its designed to make handling all the things described above (and lots more) very easy and comfortable.
If something is missing, please let me know. :)