PowerTip: Hide Common Code in PowerShell ISE


Summary: Learn how to hide common code in the Windows PowerShell ISE.

Hey, Scripting Guy! Question How can I hide related code in the Windows PowerShell ISE to make it easier to troubleshoot and to read?

Hey, Scripting Guy! Answer Open the ISE, and in the script pane, begin a section with the #Region tag and a name.
           Then end the section with the #EndRegion tag. Now everything between the two tags can be collapsed
           and hidden when desired. Here is an example:

#region Name

 If($d.name -match  ‘,’)

        {

          $name = (Get-Culture).textinfo.ToTitleCase($d.name).Split(‘,’).trim()

          $ln = $name[0]

          $fn = $name[1]

          }

#endregion

Comments (7)

  1. Chris J Warwick says:

    @jrv Agreed. It would be nice if they could fix that too (or at least document it 🙂

    Are you aware of any other cases?

    Anyways, it seems they’ve fixed #Requires in V5 (in the September release in any case…)

    Cheers,
    Chris

  2. Chris J Warwick says:

    @jrv DOH, sorry, prematurely exited. #Region is still not working…..

  3. Chris J Warwick says:

    @DJ – I was just going to post exactly the same comment but you beat me to it!

    Interesting that, in the descriptive text at the start of this post, the #Region and #EndRegion are typed correctly 🙂

  4. Chris J Warwick says:

    @jrv Glad it works for you, however it doesn’t work for me (and others). PowerShell is, in almost every other respect, case insensitive – and in my opinion this should not be an exception.

  5. jrv says:

    There are many examples of case sensitivity in PowerShell and in Net. All properties in AD are case sensitive when referenced as string keys.

    $user.Properties[‘name’].Value

    is one example. ‘name’ works. ‘Name’, ‘NAME’ or ‘nAme’ will not work.

  6. jrv says:

    It is not a bug. Directives are always lowercased in nearly all systems.

  7. DJ says:

    Any chance on fixing the bug in the ISE that requires [tag:region] to be lowercase? Its been a bug on connect since 2012.
    https://connect.microsoft.com/PowerShell/feedbackdetail/view/743272

Skip to main content