Prof. Powershell

PowerShell Anger Management

Here's how to make those warning messages in PowerShell more visually appealing.

When things go wrong in PowerShell, you often see an angry looking error message like this:

[Click on image for larger view.]

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.

[Click on image for larger view.]

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"

[Click on image for larger view.]

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.

[Click on image for larger view.]

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.

[Click on image for larger view.]

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.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025