Collapse This! Using Regions to Organize Your Scripts
- Written by June Blender
- Last Updated: 05 January 2017
- Created: 07 December 2016
- Hits: 13770
Whether you're writing a script or reading one, you want to keep your environment as simple and uncluttered as possible. You want to display only what you need to see and eliminate everything else. The Expand and Collapse features in PowerShell Studio let you do just that -- with simple hotkeys and configurable settings.
In this blog, I show how to configure and use the Expand and Collapse features of PowerShell Studio and how to use regions to organize the code in your scripts.
How to Expand and Collapse
By default, everything in a script is visible to you. But, when you need less clutter, collapse what you don't want to see.
- To collapse a particular region, comment block, or script block selectively, click the minus sign at the top of the block.
- To expand a collapsed region, click the plus sign beside the block header.
And, best of all...
- To collapse all regions, click Ctrl + <minus> (Ctrl + - ).
- To expand all regions, click Ctrl + <plus> (Ctrl + + ).
What's a Region?
The things that expand and collapse in PowerShell Studio are called regions. By default, PowerShell Studio automatically creates a collapsible region for:
- Comment blocks (<# ... #>), including comment-based help
- Multiline script blocks, including nested script blocks
And, you can create a custom region for any text or code in a file.
Use Regions to Organize Scripts
I often use regions to organize my code. For example, in a PowerShell GUI app script, I like to group the code that supports a feature, including script-scoped variables, event handlers, and functions. So, I create a region for it.
- To create a region by selecting, select the code and, on the Home tab, in the Edit group, click Regions, then click Create Region (Ctrl + Shift + G).
PowerShell Studio adds the region and lets you name it. Now, it's easy to expand and collapse.
You can also create a region manually.
- To create a region manually, on a blank line, type #region and an optional region name. At the end of the region, type #endregion. There are #region and #endregion snippets, too.
Here's an almost 800-line script collapsed into regions for easy viewing. This is a great aid for me and for people reviewing my code, maintaining it, and adding features to it.
Configuring Expand and Collapse
Like almost everything in PowerShell Studio, you can configure the expand and collapse features. You can enable and disable them, open scripts expanded or collapsed, and determine the elements that expand and collapse when you expand and collapse all elements.
By default, when you Expand All (Ctrl + <plus>) or Collapse All (Ctrl + <minus), regions and comment blocks expand and collapse. But, you can add Script Blocks to the set and enable and disable each element type.
To determine which elements expand and collapse:
- Click Home and, in the Edit group, click Regions, point to Include, and the click to toggle the Comments, Regions, and Script Block elements on and off.
To determine whether regions are expanded or collapsed when the script opens:
- Click Home, in the Windows group, click Options, click Editor, and then toggle Collapse regions on load.
To disable expand and collapse and eliminate all code folding regions:
- Click Home, in the Windows group, click Options, click Editor, and then uncheck Enable code folding.
Regions are a fantastic organizing tool. I like my regions collapsed on load, but I waffle between including and excluding Script Blocks from my collapse. Fortunately, PowerShell Studio lets me do that. Try this feature and let me know what you think.