PowerTip: Hide Common Code in PowerShell ISE

PowerTip: Hide Common Code in PowerShell ISE

  • Comments 8
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Any chance on fixing the bug in the ISE that requires #region to be lowercase? Its been a bug on connect since 2012. https://connect.microsoft.com/PowerShell/feedbackdetail/view/743272

  • @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 :-)

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

  • @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.

  • 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.

  • @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

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