{"id":75911,"date":"2016-01-07T00:01:00","date_gmt":"2016-01-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/01\/07\/a-favorite-powershell-ise-feature-snippets\/"},"modified":"2019-12-11T07:27:20","modified_gmt":"2019-12-11T15:27:20","slug":"a-favorite-powershell-ise-feature-snippets","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-snippets\/","title":{"rendered":"A Favorite PowerShell ISE Feature: Snippets"},"content":{"rendered":"<p><b>Summary<\/b>: Sean Kearney shows you how to get sample code in the Windows PowerShell\u00a0ISE.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today to show you a really cool feature that has been in Windows PowerShell ISE for a while, but you might have overlooked it! It\u2019s called Snippets.<\/p>\n<p><b>\u00a0 \u00a0Note<\/b>\u00a0\u00a0\u00a0This is a five-part series that includes the following posts about features in the Windows PowerShell ISE:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-script-analyzer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Script Analyzer<\/a>\nDownload a free tool for examining your script<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-script-browser\/\" target=\"_blank\" rel=\"noopener noreferrer\">Script Browser<\/a>\nBrowse the TechNet Script Repository from the Windows PowerShell ISE<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-module-browser\/\" target=\"_blank\" rel=\"noopener noreferrer\">Module Browser<\/a>\nAccess modules in the PowerShell Gallery<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-snippets\/\" target=\"_blank\" rel=\"noopener noreferrer\">Snippets<\/a>\nGet sample code in the Windows PowerShell\u00a0ISE<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-remote-text-file-editing\/\" target=\"_blank\" rel=\"noopener noreferrer\">Remote Text File Editing<\/a>\nBring file editing to the Windows PowerShell\u00a0ISE<\/li>\n<\/ul>\n<p>I love using the Snippets feature on a regular basis, because although I can work in PowerShell, I don\u2019t memorize code. I\u2019m an IT pro, not a dev. Even my good developer friends don\u2019t memorize everything.<\/p>\n<p>The Snippets feature was actually introduced in Windows PowerShell ISE\u00a03.0, and I\u2019ve always found it to be an invaluable tool.<\/p>\n<p>Press <b>CTRL<\/b>+<b>J<\/b> on the keyboard to pull up the main list of Snippets.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7127.1.PNG\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7127.1.PNG\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>As you can see, there is a large pile of prebuilt code examples in the PowerShell ISE. If you\u2019d like to use one, simply select it from the list. You can use the arrow keys to navigate up and down the list, or you can use your mouse.<\/p>\n<p>Clicking an example immediately pastes it into the active window, whether that is the Script Panel or the Console view in the ISE. Here is an example of the <b>do-while<\/b> loop in the Snippet list and pasted into the editing window:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6303.2.PNG\"><img decoding=\"async\" title=\"Image of command\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6303.2.PNG\" alt=\"Image of command\" \/><\/a><\/p>\n<p>The cool part about the Snippets is that although Microsoft provides a strong rudimentary list, you can also add to it! Adding your custom snippets requires an example stored as a here-string and the <b>New-ISESnippet<\/b> cmdlet.<\/p>\n<p>In the following example, I am going to add a sample to remind myself how to define a string array, including some comments I should remember to edit. Here is the original code in PowerShell:<\/p>\n<p style=\"margin-left: 30px;\"># My Example String<\/p>\n<p style=\"margin-left: 30px;\">#<\/p>\n<p style=\"margin-left: 30px;\"># Some string array with a description<\/p>\n<p style=\"margin-left: 30px;\">[string[]]$SampleValue=@()<\/p>\n<p>\u2026and here is what was required to add it as a personal Snippet in my PowerShell ISE.<\/p>\n<p style=\"margin-left: 30px;\">$Text=@&#8221;<\/p>\n<p style=\"margin-left: 30px;\"># My Example String<\/p>\n<p style=\"margin-left: 30px;\">#<\/p>\n<p style=\"margin-left: 30px;\"># Some string array with a description<\/p>\n<p style=\"margin-left: 30px;\">[string[]]$SampleValue=@()<\/p>\n<p style=\"margin-left: 30px;\">&#8220;@<\/p>\n<p style=\"margin-left: 30px;\">$Title=\u201dArray Sample\u201d<\/p>\n<p style=\"margin-left: 30px;\">$Description=\u201dHow to create a simple String Array including Comments\u201d<\/p>\n<p style=\"margin-left: 30px;\">$Author=\u201dSean Kearney, Honorary Scripting Guy\u201d<\/p>\n<p style=\"margin-left: 30px;\">New-ISESnippet \u2013Text $Text \u2013Title $Title \u2013Description $Description \u2013Author $Author<\/p>\n<p>If I execute this code and press <b>CTRL<\/b>+<b>J<\/b> immediately to see the list of Snippets, I can see that this new Snippet was added:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0488.5.PNG\"><img decoding=\"async\" title=\"Image of command\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0488.5.PNG\" alt=\"Image of command\" \/><\/a><\/p>\n<p>You\u2019ll also notice that the new Snippet is an XML file within the user\u2019s personal PowerShell folder under a special folder called <b>Snippets<\/b>. A list of all personal Snippets for a user are within this folder.<\/p>\n<p>If you back up this folder, you can easily transfer any new Snippet examples between computers. As you reload your ISE each time, the new Snippets are immediately available to you.<\/p>\n<p>Of course, if you\u2019ve decided you don\u2019t like a particular example, you can easily remove it from the system by deleting its XML file or moving it out of the PowerShell Snippets folder.<\/p>\n<p>You can also (if you feel so inclined) edit the sample Snippet in the XML file. Here is the Snippet I just created. You can see the sample code within the block marked &lt;Code&gt; and &lt;\/Code<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1007.6.PNG\"><img decoding=\"async\" title=\"Image of script\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1007.6.PNG\" alt=\"Image of script\" \/><\/a><\/p>\n<p>You can simple edit this file within any standard text editor, such as Notepad.<\/p>\n<p>Sure, Snippets is an older feature, but it\u2019s one of those nice things that makes the built-in ISE nice to use for an IT professional.<\/p>\n<p>Tomorrow, I\u2019ll wrap up this week about features I love to use in the Windows PowerShell ISE. I\u2019ll be talking about remote text file editing with the <b>PSEdit<\/b> cmdlet.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener noreferrer\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener noreferrer\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy, Cloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Sean Kearney shows you how to get sample code in the Windows PowerShell\u00a0ISE. Honorary Scripting Guy, Sean Kearney, is here today to show you a really cool feature that has been in Windows PowerShell ISE for a while, but you might have overlooked it! It\u2019s called Snippets. \u00a0 \u00a0Note\u00a0\u00a0\u00a0This is a five-part series that [&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":[56,154,659,45],"class_list":["post-75911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-snippets","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Sean Kearney shows you how to get sample code in the Windows PowerShell\u00a0ISE. Honorary Scripting Guy, Sean Kearney, is here today to show you a really cool feature that has been in Windows PowerShell ISE for a while, but you might have overlooked it! It\u2019s called Snippets. \u00a0 \u00a0Note\u00a0\u00a0\u00a0This is a five-part series that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75911","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=75911"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75911\/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=75911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}