{"id":74501,"date":"2015-11-12T00:01:00","date_gmt":"2015-11-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/12\/use-regions-in-powershell-ise\/"},"modified":"2019-02-18T09:34:33","modified_gmt":"2019-02-18T16:34:33","slug":"use-regions-in-powershell-ise","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-regions-in-powershell-ise\/","title":{"rendered":"Use Regions in PowerShell ISE"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using regions in the Windows PowerShell ISE.<\/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! I like using the Windows PowerShell ISE. The price is right I guess. There is one thing that kind of bugs me, however. &nbsp;When I am writing a long script, there is too much scrolling, and things seem to slow down. I wish there was a way that I did not have to deal with lines and lines of script. I guess I can move the functions into a module, but that adds more complexity than I want in my life right now. Is there something that can help?<\/p>\n<p>&mdash;RR<\/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 RR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the features added to the Windows PowerShell ISE in Windows PowerShell&nbsp;3.0 was the region feature. It permits me to mark sections of the script that will collapse or expand as I wish&mdash;depending on whether I turn on or turn off outlining mode. I can do this by selecting <b>Show Outlining<\/b> <strong>(Regions)<\/strong> from the <b>View<\/b> menu. But dude, that requires using a mouse. I prefer to use Windows PowerShell to control the Windows PowerShell ISE.<\/p>\n<p>I can create a region in the Windows PowerShell ISE by doing two things. The first is to use the pound sign, the region keyword, and a description of the region. The second is to end the region section by using a pound sign and the <b>endregion<\/b> (all one word) keyword. This is shown here:<\/p>\n<p style=\"margin-left:30px\">#region begin regiiin<\/p>\n<p style=\"margin-left:30px\">#endregion<\/p>\n<p>That is all there is to it. Begin and end the region. The following image shows s script with three regions marked in it:<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/hsg_2D00_11_2D00_12_2D00_15_2D00_01.png\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/hsg_2D00_11_2D00_12_2D00_15_2D00_01.png\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>The thing that is a bit confusing is that the <b>#region<\/b> and <b>#endregion<\/b> look like comments in the code. To collapse a region, I click the minus sign ( <b>&#8211; <\/b>) that appears beside the <b>#region<\/b> command:<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/hsg_2D00_11_2D00_12_2D00_15_2D00_02.png\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/hsg_2D00_11_2D00_12_2D00_15_2D00_02.png\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>When the region collapses, the minus sign changes to a plus sign ( <b>+<\/b> ). This change indicates that the region is collapsed, and that there is more code hidden in the region.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> It is important for readability of code that the region names make sense and describe what the region contains. This permits you to quickly find the code required.<\/p>\n<p>I can quickly collapse or expand all regions by using the <b>ToggleOutliningExpansion()<\/b> method. This method is available from the <b>Editor<\/b> object of the current file. The command is shown here:<\/p>\n<p style=\"margin-left:30px\">$psISE.CurrentFile.Editor.ToggleOutliningExpansion()<\/p>\n<p>When I toggle all of the closed regions, the script still runs. In fact, toggling or expanding a region in a script has no effect on run ability&mdash;only on readability. The following image shows the command I used to collapse all of the regions. It also shows that when I run the script, the commands still execute.<\/p>\n<p><a href=\"\/cfs-file.ashx\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/4035.hsg_2D00_11_2D00_12_2D00_15_2D00_03.png\"><img decoding=\"async\" src=\"\/resized-image.ashx\/__size\/550x0\/__key\/communityserver-blogs-components-weblogfiles\/00-00-00-76-18\/4035.hsg_2D00_11_2D00_12_2D00_15_2D00_03.png\" alt=\"Image of command output\" title=\"Image of command output\"><\/a><\/p>\n<p>Play around with regions. They should become a best practice added to your script repository. They make code easier to read, and therefore, easier to troubleshoot when the time comes. And for such a slight amount of work, it can pay great dividends.<\/p>\n<p>RR, that is all there is to using regions in your Windows PowerShell ISE.&nbsp; Join me tomorrow when I will talk about more cool stuff.<\/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><span style=\"font-size:12px\">&nbsp;<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using regions in the Windows PowerShell ISE. &nbsp;Hey, Scripting Guy! I like using the Windows PowerShell ISE. The price is right I guess. There is one thing that kind of bugs me, however. &nbsp;When I am writing a long script, there is too much scrolling, and things [&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":[3,45],"class_list":["post-74501","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using regions in the Windows PowerShell ISE. &nbsp;Hey, Scripting Guy! I like using the Windows PowerShell ISE. The price is right I guess. There is one thing that kind of bugs me, however. &nbsp;When I am writing a long script, there is too much scrolling, and things [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74501","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=74501"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74501\/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=74501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}