#PSTip Use custom regions to fold code in PowerShell ISE 3.0

Note: This tip requires PowerShell 3.0 or above.

The PowerShell ISE 3.0 brought a lot of new features. One of them is the ability to hide parts of the code by folding them. The foldable code is marked by small minus/plus signs and can easily fold and unfold by clicking the sign.

In this example the parts of the code were marked for folding automatically, but it is also possible to manually define a foldable region. To mark start of the region use ‘#region’ keyword optionally followed by name. To mark the end use simply #endregion. In the following example I define one around all the function
definitions to hide them easily:


It is also possible to add name of the region you are ending after the #endregion comment, but keep in mind the name is in this case just a memo, not part of the syntax. See the following example:

I am trying to end the region ‘one’ before the region ‘two’ ended, but regardless of the name specified the region called two is terminated.

Another thing to keep in mind is the #region and #endregion are case sensitive in the PowerShell ISE; specifying #Region or #endRegion unfortunately won’t work. PowerGUI and PowerShell Plus are more forgiving as they don’t care about the case.

Filed in: Columns, Tips and Tricks Tags: , , ,

7 Responses to "#PSTip Use custom regions to fold code in PowerShell ISE 3.0"

  1. Tony says:

    A problem I’ve found with collapsed regions is the colour of the remaining text. I use a dark colour theme in my ISE and so the collapsed regions are almost impossible to read because they are black.

    There doesn’t seem to be any way to change it. A slight oversight in the colour settings.

  2. Darek says:

    Hi there. I have the same issue with the region’s color setting as Tony: it’s completely invisible when collapsed. Is there a way to correct it? Thanks.

  3. redcloud says:

    Thank you! i did not know it was case sensitive now!

  4. S Gandhi says:

    Did anyone find , how to change region color when collapsed in dark background?

  5. Sito says:

    bump….still can’t find anything on how to change the region text color when it is collapsed.

Leave a Reply

Submit Comment

© 8086 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: