
close
close
Upcoming FREE Conference on Identity Management and Privileged Access Management
I don’t like typing PowerShell scripts any more than the rest of you, so I rely extensively on snippets to save time. Microsoft includes a fair number of snippets out of the box, where you can easily access them in the PowerShell ISE by pressing Ctrl+J to display a popup list. Scroll to the one you want and press enter. In this article, I’ll teach you how to see and use different snippets that you already have available in PowerShell.
In the PowerShell ISE, you can use the PSISE object model to access the current PowerShell tab. Each tab can have its own set of snippets.
Each PowerShell tab has its own snippets. (Image Credit: Jeff Hicks)
Viewing snippet information in PowerShell. (Image Credit: Jeff Hicks)
$psise.CurrentPowerShellTab.Snippets | Sort DisplayTitle | select DisplayTitle,Description | out-gridview -title "My Snippets"
Viewing available snippets in GridView. (Image Credit: Jeff Hicks)
The Get-ISESnippet cmdlet lists the snippet files in the default location. (Image Credit: Jeff Hicks)
Searching for a snippet with the PowerShell Get-ISESnippet cmdlet. (Image Credit: Jeff Hicks)
#requires -version 4.0 #requires -module ISE <# This is a copy of CommandType Name ModuleName ----------- ---- ---------- Function Get-IseSnippet ISE Created: 5/15/2015 Author : Jeff **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** #> Function Get-MySnippet { <# .SYNOPSIS Gets snippets that the user created. .DESCRIPTION The Get-MySnippet cmdlet gets the PS1XML files that contain reusable text "snippets" that the user created. It works only in Windows PowerShell ISE. When you use the New-IseSnippet cmdlet to create a snippet, New-IseSnippet creates a <SnippetTitle>.Snippets.ps1xml file in the $home\Documents\WindowsPowerShell\Snippets directory. Get-MySnippet gets the snippet files in the Snippets directory. Get-MySnippet does not get built-in snippets or snippets that are imported from modules by using the Import-IseSnippet cmdlet. This cmdlet is introduced in Windows PowerShell 3.0. This specific command is a modified version of Get-ISESnippet. .PARAMETER Name The name of a snippet to get. Wildcards are allowed. The default is all snippets. .EXAMPLE PS C:\>Get-MySnippet This command gets all user-defined snippets in the Snippets directory. .EXAMPLE PS C:\> get-mysnippet validate* | out-gridview -PassThru | foreach { psedit $_.fullname} Get all snippets that start with 'Validate' and send the results to Out-Gridview. Select snippets will be opened in the ISE for editing. .NOTES The New-IseSnippet cmdlet stores new user-created snippets in unsigned .ps1xml files. As such, Windows PowerShell cannot add them to a session in which the execution policy is AllSigned or Restricted. In a Restricted or AllSigned session, you can create, get, and import unsigned user-created snippets, but you cannot use them in the session. To use unsigned user-created snippets that the Get-MySnippet cmdlet returns, change the execution policy, and then restart Windows PowerShell ISE. For more information about Windows PowerShell execution policies, see about_Execution_Policies. Learn more about PowerShell:Essential PowerShell Learning Resources**************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** .INPUTS None .OUTPUTS System.IO.FileInfo custom object .LINK Get-IseSnippet New-IseSnippet #> [CmdletBinding()] Param( [Parameter(Position=0,HelpMessage="Enter the name of snippet")] [ValidateNotNullorEmpty()] [string]$Name = "*", [switch]$Detailed ) Begin { Write-Verbose "Starting $($MyInvocation.Mycommand)" Write-Verbose "Using parameter set $($PSCmdlet.ParameterSetName)" Write-Verbose ($PSBoundParameters | Out-String) #remove Name from Boundparameters if ($PSBoundParameters.Name) { $PSBoundParameters.Remove("Name") | Out-Null } #remove Detailed from Boundparameters if ($PSBoundParameters.Detailed) { $PSBoundParameters.Remove("Detailed") | Out-Null } #define a snippet namespace $snipns = @{snip="http://schemas.microsoft.com/PowerShell/Snippets"} } #begin Process { $results = (Get-IseSnippet @PSBoundParameters).Where({$_.name -like "$name.snippets.ps1xml"}) if ($Detailed) { #get detailed output foreach ($item in $results) { $author = (Select-XML -Path $item.fullname -Namespace $snipns -XPath "//snip:Author").Node.InnerText $title = (Select-XML -Path $item.fullname -Namespace $snipns -XPath "//snip:Title").Node.InnerText $description = (Select-XML -Path $item.fullname -Namespace $snipns -XPath "//snip:Description").Node.InnerText #create a custom object New-Object -TypeName PSObject -Property @{ Path = $item.Fullname Author = $author Title = $title Description = $description Modified = $item.LastWriteTime } } #foreach } #if detailed else { #write regular results to the pipeline $results } } #process End { Write-Verbose "Ending $($MyInvocation.Mycommand)" } #end } #end function Get-MySnippet
In my version, I included a parameter to filter by file name including the use of wildcards.
Filtering by name with wildcards. (Image Credit: Jeff Hicks)
$snipns = @{snip="http://schemas.microsoft.com/PowerShell/Snippets"}
$author = (Select-XML -Path $item.fullname -Namespace $snipns -XPath "//snip:Author").Node.InnerText
The XPath query will return node objects. The InnerText property should contain the values I want. I should point out that it is technically possible to have multiple snippets in a single snippet file. My technique will most likely fail in that scenario. If you stick to one snippet per file, then this won’t be an issue. After I get all of the details, I write a custom object to the pipeline for each snippet file.
New-Object -TypeName PSObject -Property @{
Path = $item.Fullname
Author = $author
Title = $title
Description = $description
Modified = $item.LastWriteTime
}
Get-MySnippet results. (Image Credit: Jeff Hicks)
Get-MySnippet -Detailed | where {$_.description -match "references"}
Finding specific snippets in Windows PowerShell. (Image Credit: Jeff Hicks)
Get-MySnippet -Detailed | where {$_.description -match "references"} | foreach {psedit $_.path}
Get-MySnippet "Validate*" | foreach {psedit $_.fullname}
I hope my tools help you use snippets more in your own script development. Once you build up a library, you’ll be amazed at how quickly you can churn out a high-quality product.
More in PowerShell
Most popular on petri