Change the PowerShell Colors

Have you ever grown tired of looking at the different colors of the PowerShell console? Have you ever thought to change your PowerShell console colors? I have dabbled and changed them up a bit myself, but there some things to keep in mind.

At the PowerShell and DevOPs Summit earlier this year Don Jones (PowerShell MVP) brought up some good points that red error messages are difficult to read and can have negative connotations. Red ink on test scores, anyone?

Whatever the case, let’s change ’em up!

Get-Host and $Host

In order to change the colors, I’ll familiarize myself with where that info is stored.

This info is stored in the variable $host or by using the Get-Host cmdlet. These commands retrieve an object that has info about the PowerShell console itself, the console host.

Let’s see what both of those look like.

Fancy. Lots of fancy going on here.

Change the PowerShell colors

There are a lot of properties that I can dive into with the $host object, but I’m going to dive into the PrivateData property today. It has all the color goodies that I’m looking for.

$host.PrivateData

This is where the colors are set for the Errors, Warnings, Debug, Verbose, and Progress streams.

Jackpot! Let’s change the error colors for fun! I’m going to take a wild guess that White is a valid color.

$host.PrivateData.ErrorBackgroundColor = "White"

Yikes! That’s even more harsh on my eyes!  Don’t stare too long!

What else can I change it to? I might be tempted to just plug in values until I get lucky, but there’s a better way. After all, it’s PowerShell we’re talking about!

First, I’ll find out what type of object the ErrorBackgroundColor is. Then, I should be able to figure out what to do with it.

$Host.PrivateData.ErrorBackgroundColor.GetType()

It looks like we’re working with an enum called ConsoleColor. Googling this would probably lead me to find the MSDN page, but I want to be cool and use PowerShell to list the enum values.

[System.Enum]::GetValues('ConsoleColor')

Awesome.

But, I was hoping for pretty colors. Fortunately, the Write-Host cmdlet allows for displaying text in different colors, so I’ll make use of that and loop through each item.

[System.Enum]::GetValues('ConsoleColor') | ForEach-Object { Write-Host $_ -ForegroundColor $_ }

Let’s go back to the error background that we set as white and change it to the background of the window – DarkMagenta.

$host.PrivateData.ErrorBackgroundColor = "DarkMagenta"

Taking this a step further, we’ll even change the error foreground color to make it much more soothing to the eyes.

$host.PrivateData.ErrorForegroundColor = "DarkGray"

Much better! Now I don’t feel like PowerShell is shouting at me when errors occur. Sometimes I’ll switch it up between DarkGray, DarkGreen, and even DarkCyan!

I like to live on the edge.

Change the console window colors

Changing the main console foreground and background is slightly different. That data is stored in a separate location – $host.UI.RawUI

Let’s change it!

$host.UI.RawUI.ForegroundColor = "DarkGreen"
$host.UI.RawUI.BackgroundColor = "Black"

That worked! …kind of.  One interesting thing to note is that it doesn’t seem to completely refresh until you close and reopen your PowerShell window or until you clear the screen.

How wacky!

Wrapping up

Congratulations! You now have control over the colors of your PowerShell console! Woo!

Also, as a side note – you can extend this fancy colorization to the ISE. But, I think I’ll save that for another day.

Happy PowerShelling!



Follow @admarsenal on Twitter

Your email address will not be published.

Your Name