{"id":2291,"date":"2012-06-27T08:40:38","date_gmt":"2012-06-27T08:40:38","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2012\/06\/27\/snippets-in-windows-powershell-ise-3-0\/"},"modified":"2024-02-26T12:03:32","modified_gmt":"2024-02-26T20:03:32","slug":"snippets-in-windows-powershell-ise-3-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/snippets-in-windows-powershell-ise-3-0\/","title":{"rendered":"Snippets in Windows PowerShell ISE 3.0"},"content":{"rendered":"<p><em><span style=\"color: #a5a5a5;\">This blog post applies to the Windows PowerShell Integrated Scripting Environment (ISE) in Windows 8 and Windows Server 2012.\u00a0 The ISE is also fully supported and available when Server 2012 is deployed using the <a href=\"http:\/\/blogs.technet.com\/b\/windowsserver\/archive\/2012\/03\/20\/building-an-optimized-private-cloud-using-windows-server-8-server-core.aspx\">Minimal Server Interface<\/a>.\u00a0 To start ISE, type <span><span style=\"font-family: Lucida Console;\"><strong>ise<\/strong><\/span>\u00a0 <\/span>in a PowerShell console and press <span style=\"font-family: Lucida Console;\"><strong>Enter<\/strong><\/span><\/span><\/em><\/p>\n<p>Snippets provide a convenient way to paste arbitrary text into the ISE console and script editor, right where you need it.\u00a0 The idea is to provide improved productivity, discoverability, and usability:<\/p>\n<ul>\n<li>To save you from typing repetitive or long text (or hunting for the script to copy-and-paste from)<\/li>\n<li>To save you a trip to Get-Help (think about all the attributes on script cmdlets)<\/li>\n<li>Beginners may insert an if statement as a means of learning PowerShell syntax<\/li>\n<\/ul>\n<p>For those of you who are familiar with Visual Studio\u2019s Snippets, the ISE version uses the same shortcut key, but it will surely feel like a \u201cpoor man\u2019s version\u201d :-), lacking the ability to tab between fields, among other things.<\/p>\n<p>You can list available Snippets by pressing <span style=\"font-family: Lucida Console;\"><strong>Ctrl<\/strong><\/span>+<strong><span style=\"font-family: Lucida Console;\">J<\/span><\/strong> or selecting \u201cStart Snippets\u201d from the \u201cEdit\u201d menu.\u00a0 Notice that a tooltip displays the actual text which will be inserted, as you move over the selections in the Snippets drop-down.\u00a0 Here\u2019s what it looks like:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/6835.image_thumb_72E37807.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20397\" src=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/6835.image_thumb_72E37807.png\" alt=\"Image 6835 image thumb 72E37807\" width=\"425\" height=\"234\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/6835.image_thumb_72E37807.png 425w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/6835.image_thumb_72E37807-300x165.png 300w\" sizes=\"(max-width: 425px) 100vw, 425px\" \/><\/a><\/p>\n<p>There are 3 types of snippets:<\/p>\n<ul>\n<li>Default snippets, which ship with PowerShell ISE 3.0<\/li>\n<li>User-defined snippets<\/li>\n<li>Module-based snippets<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Default Snippets<\/h3>\n<p>Consider these to be the \u201cstarter set\u201d of snippets: PowerShell code that is either frequently used, or hard to discover.\u00a0 Press <span style=\"font-family: Lucida Console;\"><strong>Ctrl<\/strong><\/span>+<strong><span style=\"font-family: Lucida Console;\">J<\/span><\/strong> to start Snippets and look through the various default snippets.<\/p>\n<p>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\u2019ll be pleased to know that you can hide the default snippets, either through the Options dialog (at the bottom of the \u201cGeneral Settings\u201d tab), or directly through the ISE object model at the command line:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/2604.image_thumb_27A87F98.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20398\" src=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/2604.image_thumb_27A87F98.png\" alt=\"Image 2604 image thumb 27A87F98\" width=\"321\" height=\"17\" srcset=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/2604.image_thumb_27A87F98.png 321w, https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/2604.image_thumb_27A87F98-300x17.png 300w\" sizes=\"(max-width: 321px) 100vw, 321px\" \/><\/a><\/p>\n<h3>User-Defined Snippets<\/h3>\n<p>You can create your own snippets and add them to the snippets drop-down list using the <span style=\"font-family: Lucida Console;\">New-IseSnippet<\/span> cmdlet.\u00a0 Here\u2019s the cmdlet syntax, using one of my favorite new features: Show-Command:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/4035.image_thumb_63F665E4.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-20400\" src=\"https:\/\/devblogs.microsoft.com\/powershell\/wp-content\/uploads\/sites\/30\/2012\/06\/4035.image_thumb_63F665E4.png\" alt=\"Image 4035 image thumb 63F665E4\" width=\"214\" height=\"240\" \/><\/a>\n<em>Note that you can also type New-IseSnippet anywhere in the ISE, and then press <span style=\"font-family: Lucida Console;\">Ctrl+F1<\/span> when the caret (you may be calling it \u201ccursor\u201d) is anywhere within the cmdlet string.<\/em><\/p>\n<p>Here\u2019s an example: I often find myself wanting to show the definition (body) of a function.\u00a0 for that, I would have to type <span style=\"font-family: Lucida Console;\">Get-Command<\/span>, followed by the function name, wrap the whole thing in parentheses, and then append <span style=\"font-family: Lucida Console;\">.Definition<\/span>.\u00a0 A snippet comes in handy:<\/p>\n<p><span style=\"font-family: Lucida Console;\">New-IseSnippet -Title &#8220;Show Definition&#8221; -Description &#8220;Shows command definition&#8221; -Text &#8220;(Get-Command ).Definition&#8221; -CaretOffset 13<\/span><\/p>\n<p>This example adds a \u201cShow Definition\u201d snippet to the snippets list, and inserts the following text when selected:\u00a0\u00a0 <span style=\"font-family: Lucida Console;\">(Get-Command ).Definition<\/span><\/p>\n<p>Notes:<\/p>\n<ul>\n<li>The caret is placed 13 characters after the start of the snippet text (ready for you to enter the command name).\u00a0 This is what the \u2013CaretOffset parameter is for<\/li>\n<li>Example usage: <span style=\"font-family: Lucida Console;\">(Get-Command prompt).Definition <\/span><\/li>\n<li>To see all user-defined snippets, type: <span style=\"font-family: Lucida Console;\">Get-IseSnippet<\/span><\/li>\n<li>To remove a snippet, go to the folder which contains all user-defined snippets (available from <span style=\"font-family: Lucida Console;\">Get-IseSnippet<\/span>) and delete the corresponding snippet file.<\/li>\n<\/ul>\n<p>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 \u201chome\u201d folder, which you can get using one of the following methods:<\/p>\n<ul>\n<li><span style=\"font-family: Lucida Console;\">Join-Path (Split-Path $profile.CurrentUserCurrentHost) &#8220;Snippets&#8221;<\/span><\/li>\n<li>Type <span style=\"font-family: Lucida Console;\">Get-IseSnippet<\/span> and copy the displayed path \uf04a<\/li>\n<\/ul>\n<p>Any snippets in the snippets home folder will be loaded automatically by the ISE when it starts.\u00a0 If you don\u2019t 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: <span style=\"font-family: Lucida Console;\">Import-IseSnippet -Path &#8216;C:\\Temp&#8217;<\/span><\/p>\n<p>Snippets files have an extension of .snippets.ps1xml and have the following structure:<\/p>\n<blockquote><p>&nbsp;<\/p>\n<p>&lt;?xml version=&#8217;1.0&#8242; encoding=&#8217;utf-8&#8242; ?&gt;\n&lt;Snippets\u00a0 xmlns=&#8217;<a href=\"http:\/\/schemas.microsoft.com\/PowerShell\/Snippets'\">http:\/\/schemas.microsoft.com\/PowerShell\/Snippets&#8217;<\/a>&gt;\n&lt;Snippet Version=&#8217;1.0.0&#8242;&gt;\n&lt;Header&gt;\n&lt;Title&gt;Show Definition&lt;\/Title&gt;\n&lt;Description&gt;Shows command definition&lt;\/Description&gt;\n&lt;Author&gt;&lt;\/Author&gt;\n&lt; SnippetTypes&gt;\n&lt; SnippetType&gt;Expansion&lt;\/SnippetType&gt;\n&lt;\/SnippetTypes&gt;\n&lt;\/Header&gt;<\/p>\n<p>&lt;Code&gt;\n&lt;Script Language=&#8217;PowerShell&#8217; CaretOffset=&#8217;13&#8217;&gt;\n&lt;![CDATA[(Get-Command ).Definition]]&gt;\n&lt;\/Script&gt;\n&lt;\/Code&gt;<\/p>\n<p>&lt;\/Snippet&gt;\n&lt; \/Snippets&gt;<\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<p>Where:<\/p>\n<ul>\n<li>Title: The name which appears in the snippets drop-down list<\/li>\n<li>Description: The description which appears in the snippet tooltip<\/li>\n<li>Cdata: The tag which contains the actual snippet text<\/li>\n<\/ul>\n<h3>Module-Based Snippets<\/h3>\n<p>To load all module-based snippets (which only works for modules which have already been imported using <span style=\"font-family: Lucida Console;\">Import-Module<\/span>), use: <span style=\"font-family: Lucida Console;\">Import-IseSnippet -Module Xyz<\/span><\/p>\n<p>If you\u2019re shipping a module with snippets, place your snippet files into a \u201cSnippets\u201d folder, directly under the module folder.<\/p>\n<p>&nbsp;<\/p>\n<p>I hope that this quick walk-through will encourage you to use snippets, create your own, and share them with the community.<\/p>\n<p>&nbsp;<\/p>\n<p>ref@<\/p>\n<p>Refaat Issa<\/p>\n<p>Program Manager, Windows Server Manageability<\/p>\n<p>Microsoft Corporation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post applies to the Windows PowerShell Integrated Scripting Environment (ISE) in Windows 8 and Windows Server 2012.\u00a0 The ISE is also fully supported and available when Server 2012 is deployed using the Minimal Server Interface.\u00a0 To start ISE, type ise\u00a0 in a PowerShell console and press Enter Snippets provide a convenient way to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[260,352],"class_list":["post-2291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-powershell-ise","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>This blog post applies to the Windows PowerShell Integrated Scripting Environment (ISE) in Windows 8 and Windows Server 2012.\u00a0 The ISE is also fully supported and available when Server 2012 is deployed using the Minimal Server Interface.\u00a0 To start ISE, type ise\u00a0 in a PowerShell console and press Enter Snippets provide a convenient way to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=2291"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}