Prof. Powershell
PowerShell Anger Management
Here's how to make those warning messages in PowerShell more visually appealing.
- By Jeffery Hicks
- 08/06/2013
When things go wrong in PowerShell, you often see an angry looking error message like this:
I mistyped the service name and PowerShell gives me a mean looking message formatted as red text on a black background. We know that red is important and should draw our attention, but many of the students I have had over the year simply ignore the message and plow on. When in reality the message can provide useful information. In this case it tells me very clearly it can't find the service called 'wuausrv'.
One solution I think is to change the color scheme PowerShell uses to display these error messages. This information is stored in the built-in $host variable as part of 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
The top two entries are the relevant settings. So why not change this into something more pleasing? You can use any of the console colors you would use with Write-Host. Let's change the foreground color to Green.
PS C:\> $host.PrivateData.ErrorForegroundColor = 'green'
You can use single or double quotes. PowerShell won't care. Now look what happens when an error occurs.
Perhaps I'm more likely to read the error message now. This will work in any version of PowerShell. You could also change the background color as well.
PS C:\> $host.PrivateData.ErrorBackgroundColor="DarkRed"
It might be tricky finding the correct combination, so here's a little PowerShell code you can run from either the console or the ISE to go through every combination of foreground and background colors.
$colors = [enum]::GetNames([consolecolor])
foreach ($fg in $colors) {
foreach ($bg in $colors) {
Write-Host "This is $fg on $bg" -ForegroundColor $fg -BackgroundColor $bg
}
}
I'll leave it to you to try this out to see the results.
Another option, is to simply not use a background color for the error messages. Unfortunately, you can't simply set the backgroundcolor to nothing. You have to specify a color. So to make it "disappear" all we need to do is set the error background color to the same as the PowerShell window. That information is stored here:
PS C:\> $host.ui.rawui.BackgroundColor
DarkMagenta
Even though I have what looks like a dark blue background, as far as PowerShell is concerned it is DarkMagenta. Anyway, now I can set this for error messages.
PS C:\> $host.PrivateData.ErrorBackgroundColor = $host.ui.rawui.BackgroundColor
PS C:\> $host.PrivateData.ErrorForegroundColor = "green"
This is definitely easier to read.
These settings will only last for as long as my PowerShell session is open. So if I wanted to make this more permanent I would need to put these lines in my profile script.
Don't forget the PowerShell ISE can also be configured to display errors in a different color scheme. You can use the same command line techniques. Or in PowerShell 3.0 ISE you can modify colors under Tools – Options – Colors and Fonts.
As you can see in Figure 5 you can adjust colors with even more options so I'm sure you can find some pleasant combination that won't make you feel like you've angered PowerShell whenever you make a simple, and human mistake.
About the Author
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.