Make Your PowerShell Errors Less Harsh By Changing Their Color

Ok, so errors are never actually pleasing regardless of what color they are. But maybe you’ve been working with PowerShell for a while and or are just dipping your toes in the water and for one reason or another, you are seeing more red than anything else on your screen.

image

Wouldn’t it be great to have a slightly less intimidating color to display an error message to you? If your answer is yes, then continue reading. If no, well… move along, nothing to see here.

The answer to how to change the color of the error message lies in PrivateData property of $host (which is an automatic variable which represents the current host application for PowerShell). Let’s take a look at what is currently set in this object.

$host.privatedata

 image

As you can see, each type of stream; Error, Warning,Debug,Verbose and Progress has both a Background and Foreground color assigned to it by default. Luckily for us, these can be changed rather easily. But what colors can we choose from? Let’s find out.

[enum]::GetNames([System.ConsoleColor])

image

Changing the error foreground color is as easy as

$host.PrivateData.ErrorForegroundColor = 'White'

image

There we go, now those error messages are not so intimidating to us. Pretty simple, isn’t it? Now you have a way to make not only the errors displayed in a different color, but even verbose, warning, etc… Just add some lines to your PowerShell profile and you are on your way!

This entry was posted in powershell and tagged , , , . Bookmark the permalink.

4 Responses to Make Your PowerShell Errors Less Harsh By Changing Their Color

  1. peaches says:

    Thanks a bunch! This was very helpful.

  2. Mike says:

    Great tip Boe. I’ve been reading about Don Jones and the puppy killers of Write-Host, but wanted a colour other than feint grey.

  3. tuco says:

    Thanks for the tip. Coming from years of using UNIX shells, Microsoft’s Powershell, while powerful, still just doesn’t have that good ol’ command line feel. Bloated commands to type, fixed width shell dialogs, errors that don’t even know what your current background color is and lots of why-not-just-do-it-like-UNIX things for people who have to work in a mixed environment.

    To keep changes permanent, maybe just put it in the file your $PROFILE environment variable points to.

  4. awed1 says:

    I’m not sure if my last comment made it through. Is there a way to make the changes permanent? When I restart PowerShell, the red returns.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s