Color My PowerShell World
Red can be disturbing at times, so let's change the color of the PowerShell console so it looks less aggressive when you chance upon the rare error.
- By Jeffery Hicks
I was listening to MVP and PowerShell expert Don Jones talk about his experiences training people on PowerShell. At issue is the red font used to display error messages. Red has such a strong, negative connotation that it often puts people off. Not to mention, the red text on a black background can be difficult to read.
What Don does early in the class is to have students change the font color so that errors are easier on the eyes and not so "aggressive." So, that's the lesson this week.
The $host variable represents your PowerShell host. There are a number of properties you can query and modify. The values for the error message font color, among others, can be found in the PrivateData property:
PS C:\> $host.privatedata
ErrorForegroundColor : Red
ErrorBackgroundColor : Black
WarningForegroundColor : Yellow
WarningBackgroundColor : Black
DebugForegroundColor : Yellow
DebugBackgroundColor : Black
VerboseForegroundColor : Yellow
VerboseBackgroundColor : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan
You can see that the error background color is black and the foreground color is Red. Let's change it to Green.
PS C:\> $host.privatedata.ErrorForegroundColor="Green"
Figure 1 shows the end result.
Figure 1. The PowerShell errors of my ways in green.
I kept the background color, but you can change it to any color you want. Personally, I'd probably change it to the same as the console color so that the end result is just green text. You can get that color from the RawUI property:
PS C:\> $host.ui.rawui.BackgroundColor
The color name looks odd if you are using the "blue" console. Regardless I can change the error background color to match:
PS C:\> $host.privatedata.errorbackgroundcolor= $host.ui.rawui.BackgroundColor
Now my errors are all in green (see Fig. 2).
Figure 2. Now, your errors don't look so aggro...
You can use any color option that you get with Write-Host. Here's a shortcut to list those options:
PS C:\> [system.consolecolor]::GetNames("consolecolor")
Any changes you make exist only for the length of your PowerShell session so if you want to make them "permanent" you'll need to add the lines to your PowerShell profile.
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.