{"id":7471,"date":"2015-03-05T00:01:00","date_gmt":"2015-03-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/05\/dont-write-scripts-use-snippets\/"},"modified":"2019-02-18T10:30:24","modified_gmt":"2019-02-18T17:30:24","slug":"dont-write-scripts-use-snippets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/dont-write-scripts-use-snippets\/","title":{"rendered":"Don&#8217;t Write Scripts: Use Snippets"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell snippets to avoid scripting demands.<\/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\" \/>&nbsp;Hey, Scripting Guy! One of the things that I find myself having to do from time-to-time is write scripts. I really don&rsquo;t like to write scripts, but sometimes I cannot find what I need to, and I cannot avoid it. I really wish that when I do have to write a script, I could make it as painless as possible. Is painless possible?<\/p>\n<p>&mdash;RA<\/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\" \/>&nbsp;Hello RA,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well tonight is the <a href=\"http:\/\/www.meetup.com\/Charlotte-PowerShell-Users-Group\" target=\"_blank\">Charlotte PowerShell Users Group<\/a> meeting. We will be having a guest presentation by Microsoft PFE, Jason Walker. Jason is a really cool dude, and he has written quite a few guest <a href=\"\/b\/heyscriptingguy\/archive\/tags\/jason+walker\/\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a> (in fact, he is an Honorary Scripting Guy). So, the meeting tonight will be awesome. The Scripting Wife and I have been looking forward to it for a while.<\/p>\n<p>RA, something else that is awesome is using Windows PowerShell ISE snippets to reduce the amount of scripting involved in creating a custom solution.<\/p>\n<p>When I open the Windows PowerShell ISE, the first thing I need to do is start the snippets. To do this, I can type &lt;CTRL +J&gt;, or simply select <b>Start Snippets<\/b> from the <b>Edit<\/b> menu:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-01.png\" alt=\"Image of menu\" width=\"350\" height=\"353\" title=\"Image of menu\" \/><\/a><\/p>\n<p>The snippet drop-down list appears and it shows me what snippets are available. The list is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-02.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>If I hover my mouse over the snippet for a few seconds, the actual code that comprises the snippet appears. This will appear on the left if it is a long piece of code, such as the complete advanced function. It will appear on the right if it is relatively short, such as the comment block. Here is the comment-block snippet:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-03.png\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<p>If I want to insert the code snippet into my script, I highlight the snippet from the drop-down list, and press ENTER. The current insertion point in the script governs where the code snippet will appear. This means that I need to place my mouse cursor exactly where I want the snippet to appear in my script. So the following comment block appears at the beginning of my script because that is where my cursor is:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The cool thing about this snippet is that it moved my insertion point to the place where I would more than likely actually type. Of course, the fact that I have a comment in the middle of a comment block is a bit strange&mdash;but hey, it works, and it helps me avoid typing.<\/p>\n<p>Now I decide to add a <b>For<\/b> loop. I move my insertion point after my comment block, and start the snippets again (I like to use &lt;CTRL + J&gt; because it keeps my hands on the keyboard). I select <b>For<\/b> from the drop-down list. The description tells me it is a <b>For<\/b> loop. I press ENTER, and voila! The <b>For<\/b> loop appears in my code.<\/p>\n<p>The strange thing is that I am left on the <b>For<\/b> line, not in the code block. But I guess that makes sense because most of the time I am not going to loop 99 times. I add the output for <b>$i<\/b> into the script block, and I press the green triangle (or &lt;F5&gt;) to run my script. The output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I place my insertion point after the <b>$i<\/b> that I added to the script, start the snippets again, and add an <b>If <\/b>statement to my script. I have to change the <b>$x<\/b> variable to <b>$i<\/b>, and set a condition that <b>$i<\/b> is equal to 5. I then add code in the script block so that the script will break when the <b>$i<\/b> condition matches. I then run the script again. The script and the output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-5-15-06.png\" alt=\"Image of command output\" width=\"550\" height=\"502\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So I did very little typing, and in less than a minute, I had a script &ldquo;that does something.&rdquo; Windows PowerShell code snippets offer two advantages:<\/p>\n<ol>\n<li>They help if one cannot remember exact syntax. In this way, they often provide enough so that one can quickly get the code written without having to look up everything.<\/li>\n<li>They help an experienced scripter avoid a lot of typing. In this way, the snippets help save time.<\/li>\n<\/ol>\n<p>RA, that is all there is to using Windows PowerShell ISE snippets. Script Without Scripting Week will continue tomorrow when I will talk about creating snippets.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell snippets to avoid scripting demands. &nbsp;Hey, Scripting Guy! One of the things that I find myself having to do from time-to-time is write scripts. I really don&rsquo;t like to write scripts, but sometimes I cannot find what I need to, and I cannot avoid [&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":[553,3,4,45],"class_list":["post-7471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-ise","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell snippets to avoid scripting demands. &nbsp;Hey, Scripting Guy! One of the things that I find myself having to do from time-to-time is write scripts. I really don&rsquo;t like to write scripts, but sometimes I cannot find what I need to, and I cannot avoid [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7471","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=7471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7471\/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=7471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}