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-
AuthenticodeSignatureto 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’serror messages, verbose messages, debug messages, and progress indicators are designed to stand out from anything else that appears in the console window:
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
saidyou 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-Hostcmdlet (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
wantan 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
Well, what do you
know?
PrivateDataturns 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
canchange 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"
As you can see,
there’s nothing too fancy here: we simply set the value of the
WarningBackgroundColor
property to
redand the value of the
WarningForegroundColor
property to
white. Now, let’s see what happens the next time we issue a
warning message:
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"
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. The server encountered an internal error or
misconfiguration and was unable to complete
your request. Please contact the server administrator at
webmaster@systemmanager.forsenergy.ru to inform them of the time this error occurred,
and the actions you performed just before this error. More information about this error may be available
in the server error log. Additionally, a 500 Internal Server Error
error was encountered while trying to use an ErrorDocument to handle the request.
That’s nice, but what happens if you
dochange the background and foreground colors of the console
window? In that case, these special PowerShell messages might
become far less noticeable:
ErrorBackgroundColor
:
Black
WarningForegroundColor
:
Yellow
WarningBackgroundColor
: Black
DebugForegroundColor
:
Yellow
DebugBackgroundColor
:
Black
VerboseForegroundColor
: Yellow
VerboseBackgroundColor
: Black
ProgressForegroundColor
: Yellow
ProgressBackgroundColor
:
DarkCyan
$
a.WarningForegroundColor="white"
$
a.ErrorForegroundColor= "
darkblue"
Internal Server Error