{"id":7451,"date":"2015-03-06T00:01:00","date_gmt":"2015-03-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/06\/dont-script-create-powershell-snippets\/"},"modified":"2022-06-22T13:50:47","modified_gmt":"2022-06-22T20:50:47","slug":"dont-script-create-powershell-snippets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dont-script-create-powershell-snippets\/","title":{"rendered":"Don\u2019t Script: Create PowerShell Snippets"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about creating custom Windows PowerShell snippets.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>\u00a0Hey, Scripting Guy! Thank you for yesterday&#8217;s blog post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/dont-write-scripts-use-snippets\/\" target=\"_blank\" rel=\"noopener\">Don\u2019t Write Scripts: Use Snippets<\/a>. I like the idea of having access to Windows PowerShell script snippets. But I used to create my own snippets. It made it easier for me to work, and I could create a custom script based on my snippets in less than 30 seconds. I don\u2019t see anything like that these days. Am I missing something? Please help me understand.<\/p>\n<p>\u2014JT<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0Hello JT,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning, it is raining. It looks like spring time outside. It is almost warm, and it really is damp. So, as a result, I got soggy on my way to the gym for my morning workout. To me, it just seems wrong to drive to the gym, so I always walk or run there. Anyway, after drying off and changing, I got on the treadmill to finish warming up.<\/p>\n<p>Luckily, the gym has WiFi, so I was able to check the email sent to <a href=\"&#109;&#97;&#x69;&#x6c;&#116;&#111;&#x3a;&#x73;c&#114;&#105;&#x70;&#x74;&#101;&#114;&#x40;&#x6d;&#105;&#99;&#x72;&#x6f;s&#111;&#102;&#x74;&#x2e;&#99;&#111;&#x6d;\">&#x73;c&#114;&#105;&#x70;&#x74;&#101;&#114;&#x40;&#x6d;&#105;&#99;&#x72;&#x6f;s&#111;&#102;&#x74;&#x2e;&#99;&#111;&#x6d;<\/a> when I finished my workout. JT, one of the cool things about Windows PowerShell ISE snippets, is that I can easily create my own snippets. I can even save them, and copy them to other devices. This makes them really great to use. One thing to keep in mind is that I cannot create a new snippet if the execution policy is set to <strong>AllSigned<\/strong> or <strong>Restricted<\/strong>. I will get an error message.<\/p>\n<p>Anything that I type over and over again in my Windows PowerShell scripts becomes fair game for a script snippet. Of course, the things I have a tendency to type over and over again tend to depend on what sort of a script I am writing.<\/p>\n<p>For example, if I am working with Word, I will obviously need to create an instance of the Word.Application object. If I am working on a script that connects to remote servers, I usually query Active Directory Domain Services to obtain a list of servers. So, I might need to create multiple snippets that cover the basic types of scripts I tend to write.<\/p>\n<p>The cool thing is that there is a <strong>New-ISESnippet<\/strong> cmdlet I can use to create a new snippet. I can call the cmdlet directly, or I can write a script to create the snippet. I can even overwrite an existing snippet with the same name by using the <strong>\u2013Force<\/strong> parameter. I specify the title, the description, author information, and the text. The text is the actual content of my snippet. The <strong>\u2013CaretOffset<\/strong> parameter specifies how far over the insertion point moves. This parameter requires a bit of experimentation to get it right. Here is an example of a script that creates a new ISE snippet:<\/p>\n<p style=\"margin-left:30px\">\n  New-IseSnippet -Force -Title &#8220;Basic Heading&#8221; -Description &#8220;Basic script heading info&#8221; `\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0-Author &#8220;ed wilson&#8221; -CaretOffset 18 -Text &#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# Script name:\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# Script description:\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# Script category:\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0#\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# Date:\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# Version: 1.0\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0# &#8221;\n<\/p>\n<p>When I run the script, the newly created snippet appears in the snippet list. (I use &lt;Ctrl+J> to start the snippets, but I can also click <strong>Start Snippets<\/strong> from the <strong>Edit<\/strong> menu.) The newly created snippet is shown here in the drop-down list:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-6-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-6-15-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>I select the snippet, press ENTER, and it is inserted into my new blank script file. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-6-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-6-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One thing to keep in mind is that even if I use the <strong>\u2013Force<\/strong> parameter to overwrite existing snippets, if I run the script multiple times, I will get multiple instances of the snippet appearing in the snippet drop-down list. To fix this issue, all I need to do is to close the Windows PowerShell ISE and open it. The extraneous entries no longer appear in the drop-down list.<\/p>\n<p>To delete a snippet, I use the <strong>Get-ISESnippet<\/strong> cmdlet and pipe the results to the <strong>Remote-Item<\/strong> cmdlet. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">\n  PS C:\\> Get-IseSnippet\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0 Directory: C:\\Users\\ed\\Documents\\WindowsPowerShell\\Snippets\n<\/p>\n<p style=\"margin-left:30px\">\n  Mode \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 LastWriteTime\u00a0\u00a0\u00a0\u00a0 Length \u00a0 \u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;&#8212;&#8212;&#8212;- \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#8212;&#8212; \u00a0 \u00a0&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n<\/p>\n<p style=\"margin-left:30px\">\n  -a&#8212;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3\/3\/2015\u00a0 10:33 AM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 783 Basic Heading.snippets.ps1xml \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\n<\/p>\n<p style=\"margin-left:30px\">\n  <span style=\"font-size:12px\">PS C:\\> Get-IseSnippet | Remove-Item<\/span>\n<\/p>\n<p>JT, that is all there is to creating your own script snippets. Script Without Scripting Week will continue tomorrow when I will talk about saving scripts from applications.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size:12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating custom Windows PowerShell snippets. \u00a0Hey, Scripting Guy! Thank you for yesterday&#8217;s blog post, Don\u2019t Write Scripts: Use Snippets. I like the idea of having access to Windows PowerShell script snippets. But I used to create my own snippets. It made it easier for me to work, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,553,3,4,45],"class_list":["post-7451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-ise","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating custom Windows PowerShell snippets. \u00a0Hey, Scripting Guy! Thank you for yesterday&#8217;s blog post, Don\u2019t Write Scripts: Use Snippets. I like the idea of having access to Windows PowerShell script snippets. But I used to create my own snippets. It made it easier for me to work, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7451\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}