Note: The following was tested with PowerGUI 3.8.0.129.
As I have gotten more confident with Powershell, my scripts have grown longer and longer. I use PowerGUI as my IDE of choice and I have been quite happy with it. One of the problems I’ve increasingly run into however is that as the code grows in length, it becomes harder to visually read and follow the code to pinpoint what you want to work on. I know that Powershell supports the Region/EndRegion commands but that still made it difficult to distinguish one section from another visually. I looked into alternatives but after fairly extensive Google searches was surprised to find on one else commenting on this. I did however find a PowerGUI addon called “Script Colors”. Unfortunately that didn’t offer any configuration for the region command but did discover that this script was just making modifications to an XML configuration file. I then decided I’d see if I couldn’t tackle this myself. Before we delve into how to do this though, let me show you what the end goal is.
The first screenshot below is of the default color scheme in PowerGUI. (Note the code is random gibberish and is there only to fill space for the purposes of this demonstration)
Default PowerGUI Color Scheme
There is nothing wrong with the above but let me ask you, what does the code above do? Now compare that to the screenshot below of the same code, only formatted differently:
Modified Color Scheme Highlighting Region Sections
I think you’ll agree that the second screen is much easier to read and identify what is happening as well as find the section of code you wish to modify when making modifications. So, how can you do this for yourself:
-
Open your text editor of choice (I use Notepad2) as an administrator and open the file C:\Program Files(x86)\PowerGUI\PowershellSyntax.xml
Under the <Styles> section,
- add two new entries (Starts around line 70)
<Style Key=”RegionStyle” Bold=”1″ BackColor=”#e0e0e0″/>
<Style Key=”RegionEndStyle” Bold=”1″ BackColor=”#FBFBFB”/>
- Scroll down to around line 258 and change the Style sections to apply the formatting you created above
<State Key=”RegionTagCommentState” TokenKey=”RegionTagDefaultToken” Style=”RegionStyle“>
<!– Scopes –>
<Scopes>
<Scope>
<ExplicitPatternGroup Type=”StartScope” TokenKey=”OpenRegionTag” TokenID=”60″ Style=”RegionStyle” PatternValue=”#region” LookBehind=”{LineTerminatorMacro}({WhitespaceMacro})*|\A({WhitespaceMacro})*” />
<RegexPatternGroup Type=”EndScope” TokenKey=”OpenRegionTagEndToken” TokenID=”14″ PatternValue=”{LineTerminatorMacro}” />
</Scope>
</Scopes>
<!– Pattern Groups –>
<PatternGroups>
<RegexPatternGroup TokenKey=”RegionCommentToken” TokenID=”61″ PatternValue=”{NonLineTerminatorMacro}+” />
</PatternGroups>
</State>
<!– EndRegionTagCommentState State –>
<State Key=”EndRegionTagCommentState” TokenKey=”RegionTagDefaultToken” Style=”RegionEndStyle“>
<!– Scopes –>
<Scopes>
<Scope>
<ExplicitPatternGroup Type=”StartScope” TokenKey=”CloseRegionTag” TokenID=”62″ Style=”RegionEndStyle” PatternValue=”#endregion” LookBehind=”{LineTerminatorMacro}({WhitespaceMacro})*|\A({WhitespaceMacro})*” />
<RegexPatternGroup Type=”EndScope” TokenKey=”EndRegionTagEndToken” TokenID=”15″ PatternValue=”{LineTerminatorMacro}” />
</Scope>
</Scopes>
<!– Pattern Groups –>
<PatternGroups>
<RegexPatternGroup TokenKey=”RegionCommentToken” TokenID=”61″ PatternValue=”{NonLineTerminatorMacro}+” />
</PatternGroups>
</State>
- Restart PowerGUI
One last thing to be aware of. I wasn’t able to find a way to automatically highlight the entire line as you see in the screenshot above. It’s using Regex and there are Whitespace parameters but if there is a combination that will work, I couldn’t find it. So instead I went the low tech route: I pressed tab a bunch of times after the #region tag to fill in the background color for that line. Since you don’t make many of these and once you do you don’t change them, this was an acceptable compromise for me. But if you do figure out how to make the background color apply to the entire line and not just to the present text, please tell me how.
Hope you find this useful.