• subscribe
March 23, 2010 01:30 PM

Take Control of the PowerShell Console's Colors

Windows IT Pro
InstantDoc ID #103573
Downloads
103573.zip

If you've been using PowerShell for any length of time, you might have noticed that you can control the PowerShell console's screen colors by modifying its shortcut properties, but there aren't any cmdlets that control the console colors. Cmd.exe lets you change colors easily using the Color command, but how do you do this in PowerShell? It turns out that changing colors is pretty simple, but it requires a bit more typing than in Cmd.exe.

Cmd.exe represents colors as a pair of hexadecimal digits, where the first digit is the background color and the second digit is the foreground color (i.e., the color of the text). For example, 1F represents white text on a dark blue background. To change to that color combination in Cmd.exe, you'd use the command

Color 1F

In PowerShell, you can change the console's colors by changing the BackgroundColor and ForegroundColor properties of the $HOST.UI.RawUI object. For example, the following pair of PowerShell commands is equivalent to the Color command just given:

$HOST.UI.RawUI.BackgroundColor
  = "DarkBlue"
$HOST.UI.RawUI.ForegroundColor
  = "White"

(Although these commands wrap here, you'd enter each command on one line in the PowerShell console.) You can use color name strings for the BackgroundColor and ForegroundColor properties because PowerShell automatically translates each string into the correct .NET type (System.ConsoleColor).

Figure 1 shows the 16 possible color names and their decimal and hex equivalents.



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here