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.
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
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])
Changing the error foreground color is as easy as
$host.PrivateData.ErrorForegroundColor = 'White'
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!
Thanks a bunch! This was very helpful.
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.
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.
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.