Export (0) Print
Expand All
Expand Minimize

Windows PowerShell Tip of the Week

Windows PowerShell Tip of the Week


Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Modifying Message Colors

One guiding principle behind Windows PowerShell is that the user should have a considerable amount of control over his or her working environment. For example, Windows PowerShell introduced the concept of aliases, giving you the ability to take built-in commands and give them names that are simple to type and easy to remember (on the off-chance that you don’t find names like Set-AuthenticodeSignature to be simple to type and easy to remember). Likewise, Windows PowerShell enables you to programmatically modify the title, size, and color of your console window, something we discuss in our Windows PowerShell Owner’s Manual.

Of course, while it’s nice that PowerShell lets you change the background and foreground colors of your console window, this also introduces a problem. (A tiny problem, but a problem nonetheless.) By default, PowerShell’s error messages, verbose messages, debug messages, and progress indicators are designed to stand out from anything else that appears in the console window:

Windows PowerShell Tip of the Week


That’s nice, but what happens if you do change the background and foreground colors of the console window? In that case, these special PowerShell messages might become far less noticeable:

Windows PowerShell Tip of the Week


But, then again, what are you going to do? After all, there’s no way to change the background and foreground color of a warning message or an error message.

So, anyway, about the best you can do is – hey, wait a minute: who said you can’t change the background and foreground color of a warning message or an error message?

Recolor My World

As it turns out, the Get-Host cmdlet (which, among other things, provides access to the properties of the command window) includes a child object named PrivateData. Despite the name, there’s nothing private about this object; after all, you can create an object reference to its properties and methods by using a command no more complicated than this:



$a = (Get-Host).PrivateData



So why would you even want an object reference to PrivateData? Well, suppose we echo back the value of the variable $a. When we do that we should get back information similar to this:



ErrorForegroundColor    : Red
ErrorBackgroundColor    : Black
WarningForegroundColor  : Yellow
WarningBackgroundColor  : Black
DebugForegroundColor    : Yellow
DebugBackgroundColor    : Black
VerboseForegroundColor  : Yellow
VerboseBackgroundColor  : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan



Well, what do you know? PrivateData turns out to be the place where formatting information for error messages, verbose messages, debug messages, and progress indicators resides. And guess what? By issuing a simple little command or two, we can change the appearance of any of these items.

For example, now that we have a console window featuring yellow text on a black background we might want to change the appearance of our warning messages. (Which, by default, also feature yellow text on a black background.) OK; what do you say we try white text on a red background:



$a.WarningBackgroundColor = "red"
$a.WarningForegroundColor = "white"



As you can see, there’s nothing too fancy here: we simply set the value of the WarningBackgroundColor property to red and the value of the WarningForegroundColor property to white. Now, let’s see what happens the next time we issue a warning message:

Windows PowerShell Tip of the Week


Not bad, not bad at all.

Needless to say, you can just as easily change the colors for error messages, debug messages, and progress indicators; all you have to do is assign the desired color to the appropriate property. Want to change the colors of your Windows PowerShell error messages? Then just do something along these lines:



$a.ErrorBackgroundColor = "cyan"
$a.ErrorForegroundColor = "darkblue"



Etc.

About the only thing you need to keep in mind here is that these changes are not permanent: the colors will revert to their default values when you exit and then restart PowerShell. But that’s no big deal: if you’re dead set on using custom colors for these special message types then simply add your color-changing code to your Windows PowerShell profile.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft