Prof. Powershell

Modifying PrivateData for the Public Good

Revise error messages for maximum effect through the $host object's PrivateData property.

How many times have you run a command in PowerShell only to make a mistake or run into some other kind of problem? Perhaps something like Fig. 1.

A typical error.

Figure 1. A typical error. (Click image to view larger version.)

The red text on black background screams that you screwed up and I think human nature is to simply ignore it, even though it contains valuable information in determining what went wrong. Or perhaps this color combination is hard to read. Well, you can change this, among a few other settings, to make some of PowerShell's special output easier to read.

The values that control the error message colors are stored in the $host object's PrivateData property.

PS C:\Users\Jeff> $host.PrivateData

ErrorForegroundColor    : Red
ErrorBackgroundColor    : Black
WarningForegroundColor  : Yellow
WarningBackgroundColor  : Black
DebugForegroundColor    : Yellow
DebugBackgroundColor    : Black
VerboseForegroundColor  : Yellow
VerboseBackgroundColor  : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan

As you can see, these are the colors combinations you get with commands like Write-Warning and Write-Verbose. But, you can change them. I'm going to change my errors so that they are displayed in a friendly green without any background.

PS C:\> $host.PrivateData.ErrorBackgroundColor=$host.ui.RawUI.BackgroundColor
PS C:\> $host.PrivateData.ErrorForegroundColor="Green"

I set the background color to be the same as the console background color (see Fig. 2).

A revised error message color scheme.

Figure 2. A revised error message color scheme. (Click image to view larger version.)

In fact, I'm going to go ahead and change the Warning as well (see Fig. 3):

PS C:\> $Host.PrivateData.WarningForegroundColor="White"

A revised warning message.

Figure 3. A revised warning. (Click image to view larger version.)

You can use any color that you would use with Write-Host. If you don't recall what those colors are, ask PowerShell:

PS C:\Users\Jeff> [enum]::GetNames("system.consolecolor")
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

I think this is a terrific way to make PowerShell easier to use and maybe even a little fun. However, like most everything in PowerShell, these settings are scope-specific so if you always want green error messages and white warnings, add the appropriate commands to your PowerShell profile script.

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