Last Update: Sep 04, 2024 | Published: May 27, 2015
During Microsoft Ignite, I took part in a community panel discussion on PowerShell. Well, not so much a discussion as questions and answers. One of the questions, which I get from time to time, concerned how to change the color of error messages in the PowerShell console. It is actually quite easy, and once you know the “trick”, it opens up a lot of possibilities. Before I get into it, understand that everything I am going to demonstrate is for the PowerShell console, not the PowerShell ISE. While some things might work in the ISE, it has its own settings for customizing the appearance. Perhaps I’ll cover that in another article.
In the console, this is the normal situation.
So instead of red text on a black background, you want something else. The settings are found in $host.privatedata.
The color settings are the same you would use with Write-Host. You can use the .NET framework to enumerate these values if you don’t remember them all.
To change, all you need to do is assign a new value:
Perhaps this is more appealing.
This change only lasts for the duration of your PowerShell session so if you don’t like it, you’ll get the defaults the next time you start PowerShell. But if it is something you want to keep, put the commands in your PowerShell profile script. If you want to change other color schemes for the Verbose or Warning streams, feel free to experiment. By the way, if you want to change the console foreground and/or background color, those values are found in $host.ui.rawui.
Of if you are feeling .NET-inclined you can use the [console] class.
Although as you can see, DarkYellow isn’t really yellow. But It is easy enough to change.
You will want to run Clear-Host after making changes like this.
Again, if you want this to be a persistent change, put the commands in your profile. There’s a good chance Windows will remember your preference, but it doesn’t hurt to configure in your profile.
So given, all of this, I wrote a few functions you can use to manage your color schemes. First, is a quick function to list the color names.
GET-CONSOLESCOLOR
I even added a parameter to display the values in the corresponding color.
Obviously you can’t see the color name use by the background, so I added a popup. In a similar fashion I wrote this function to show you current settings.
Show-ConsoleColor
As you experiment finding the right values, it made sense to me to include a test function.
TEST-CONSOLESCOLOR
Once you have the shell colorized the way you want it, I thought it might be nice to be able to export the values.
EXPORT-COLOR
The function will export your current color scheme to a CSV file.
Naturally, you need to be able to import the color settings.
IMPORT-CONSOLECOLOR
With these functions, if you were so inclined you could create console themes. Here’s one.
PSBRIGHT
Changing schemes is as simple as importing the CSV file.
That will wake you up! Or you can try my PSDark theme.
PSDARK.CSV
If you come up with some nice color schemes, I hope you’ll share them. In the meantime I’m sure I’ve given you enough PowerShell toys to have a little fun with. I hope you enjoy.