PowerShell Problem Solver: Make it Pretty

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.

051115 1348 PowerShellP1

So instead of red text on a black background, you want something else. The settings are found in $host.privatedata.

051115 1348 PowerShellP2

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.

051115 1348 PowerShellP3

To change, all you need to do is assign a new value:

 

Perhaps this is more appealing.

051115 1348 PowerShellP4

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.

051115 1348 PowerShellP5

Of if you are feeling .NET-inclined you can use the [console] class.

051115 1348 PowerShellP6

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.

051115 1348 PowerShellP7

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.

051115 1348 PowerShellP8

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

051115 1348 PowerShellP9

As you experiment finding the right values, it made sense to me to include a test function.

TEST-CONSOLESCOLOR

051115 1348 PowerShellP10

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.

 

051115 1348 PowerShellP11

That will wake you up! Or you can try my PSDark theme.

PSDARK.CSV

051115 1348 PowerShellP12

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.