Get-IseSnippet
Get-IseSnippet
Syntax
Get-IseSnippet [ <CommonParameters>]
Detailed Description
The Get-ISESnippet 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-ISESnippet gets the snippet files in the Snippets directory.
Get-IseSnippet 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.
Parameters
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Outputs
The output type is the type of the objects that the cmdlet emits.
- System.IO.FileInfo
Get-ISESnippet returns a file object that represents the snippet file.
Examples
-------------------------- EXAMPLE 1 --------------------------
This command gets all user-define snippets in the Snippets directory.
PS C:\>Get-ISESnippet
-------------------------- EXAMPLE 2 --------------------------
This command copies all of the user-created snippets from a group of remote computers to a shared Snippets directory.
The command uses the Invoke-Command cmdlet to run a Get-ISESnippet command on the computers in the Servers.txt file. A pipeline operator (|) sends the snippet files to the Copy-Item cmdlet, which copies them to the directory that is specified by the Destination parameter.
PS C:\>Invoke-Command -Computer (Get-Content Servers.txt) {Get-ISESnippet | Copy-Item -Destination \\Server01\Share01\Snippets}
-------------------------- EXAMPLE 3 --------------------------
This function uses the Get-ISESnippet and Select-Xml cmdlets to display the Title and Text of each snippet on the local computer.
PS C:\>#Parse-Snippet Functionfunction Parse-Snippet{$a = Get-ISESnippet$snippetNamespace = @{x="http://schemas.microsoft.com/PowerShell/Snippets"}foreach ($snippetFile in $a){Write-Host ""$Title = Select-Xml -Path $snippetFile.FullName -Namespace $snippetNamespace -XPath "//x:Title" | foreach {$_.Node.InnerXML}$Text = Select-Xml -Path $snippetFile.FullName -Namespace $snippetNamespace -XPath "//x:Script" | foreach {$_.Node.InnerText}Write-Host "Title: $Title"Write-Host "Text: $Text"}}# Sample OutputTitle: MandatoryText:Param([parameter(Mandatory=True)][String[]]$<ParameterName>)Title: CopyrightText: (c) Fabrikam, Inc. 2012
-------------------------- EXAMPLE 4 --------------------------
This command displays the title and description of all snippets in the session, including built-in snippets, user-defined snippets, and imported snippets.
The command uses the Windows PowerShell ISE object model. The $psISE variable represents the Windows PowerShell ISE host program. The CurrentPowerShellTab property of $psISE represent the current session. The Snippets property represents snippets in the current session.
The $psISE.CurrentPowerShellTab.Snippets command returns a Microsoft.PowerShell.Host.ISE.ISESnippet object that represents a snippet, unlike the Get-IseSnippet cmdlet, which returns a file object (System.Io.FileInfo) that represents a snippet file.
The command also uses the Format-Table cmdlet to display the DisplayTitle and Description properties of the snippets in a table.
PS C:\> $psISE.CurrentPowerShellTab.Snippets | Format-Table DisplayTitle, Description
Related topics