The brand new PowerShell 3.0 sports a great new ISE editor. Among many other features, it supports code folding. Code folding support is included for many control structures out of the box, for example you can fold functions or if structures.
Adding Custom Folding Regions
Sometimes, you may want to fold larger portions of your code, for example all function parameters or certain logical parts. To do this, you simply decorate the region with #region and #endregion statements.
Watch out, though: #region and #endregion are case-sensitive! You need to use all lowercase letters, or else code folding will not be enabled. Also, make sure you use #endregion and not “#end region”!
Managing Custom Folding Regions
Code regions can be nested, and in large scripts, it can become hard to manage regions and find imbalances (missing closing regions). One way of addressing this is to use labels.
You can add a label text after #region, and to make scripts more readable, you should also add the same label to the #endregion statement. This way, regions are clearly defined, and you can identify the start and ending of each region, even if you use multiple and possibly nested regions.
Doing this manually can be a drag. That’s why we developed a module for you called PTSRegion. You can download it right here:
Once you unpacked and installed the module, fire up your ISE editor and try it!
The module will do two things: it adds a new command called Update-PTSRegion, and it adds a new menu item to your ISE editor: Add-ons / Editing / Update Region. It even has a keyboard shortcut: ALT+R!
When you press ALT+R or manually run Update-PTSRegion, the currently visible script in your editor is updated. If the code has no #region statements, nothing will happen. If it does have #region statements, Update-PTSRegion will check for matching #endregions, add missing #endregion statements and correct casing.
The extension does even more: if you did not add a label to #region, it will add one for you. And it also adds the same label to the corresponding #endregion! If you did add your own label, it will use your label instead. Update-PTSRegion will work across nested regions as well.
You can play with it. Press ALT+R to update your script, then add new #region statements or change labels. Next, press ALT+R again. You’ll be surprised how much work this little helper saves, and how much more structured your #region statements will be.
Please note: Update-PTSRegion is a free helper. It is provided as-is and with no warranty. We tested it, but it may well be that it could mess up your code. So try it first, use at own risk, and let us know if you like it or whether you found issues!
If you live and work in Europe: we offer highly efficient and fun PowerShell classes and inhouse training. Working on PowerShell code refactoring tools, learning about the new ISE parser and abstract syntax tree and developing code quality tools for your company are just some of the many topics we cover. Interested? Drop me a line: [email protected].