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.

You can also use a color's numeric value instead of its name with the $HOST.UI.RawUI object. For example, the two sample PowerShell commands can also be written as

$HOST.UI.RawUI.BackgroundColor = 1
$HOST.UI.RawUI.ForegroundColor = 15 

This is less verbose than using the color names, but still a lot more verbose than Cmd.exe's Color command. So, I decided to write a pair of PowerShell scripts, Get-Color.ps1 and Set-Color.ps1, which make it faster and easier to change the PowerShell console's colors.

Listing 1 shows Get-Color.ps1.

Listing 1: Get-Color.ps1

param(\\[Switch\\] $Table)

# If -table exists, output a color list.
if ($Table) \\{
  for ($bg = 0; $bg -lt 0x10; $bg++) \\{
    for ($fg = 0; $fg -lt 0x10; $fg++) \\{
      Write-Host -nonewline -background $bg -foreground $fg `
        (" \\{0:X\\}\\{1:X\\} " -f $bg,$fg)
    \\}
    Write-Host
  \\}
  exit
\\}

# Output the current colors as a string.
" \\{0:X\\}\\{1:X\\} " -f \\[Int\\] $HOST.UI.RawUI.BackgroundColor,
  \\[Int\\] $HOST.UI.RawUI.ForegroundColor

When you run this script without parameters, it outputs a two-character hex string representing the current colors. The first hex digit in the string is the background color, and the second digit is the text color (i.e., foreground color). You can store the script's output in a variable, which makes it easy to restore the colors later if needed.

 You can run Get-Color.ps1 with the -table parameter to get a color table that displays all the color combinations (see Figure 2). You can use this table to help decide which color combination you want to use in the PowerShell console.

The Set-Color.ps1 script in Listing 2 changes the PowerShell console's colors.

Listing 2: Set-Color.ps1

param(\\[String\\] $Color = $(throw "Please specify a color."))

# Trap the error and exit the script if the user
# specified an invalid parameter.
trap \\[System.Management.Automation.RuntimeException\\] \\{
  Write-Error -errorrecord $ERROR\\[0\\]
  exit
\\}

# Assume -color specifies a hex value and cast it to a \\[Byte\\].
$newcolor = \\[Byte\\] ("0x\\{0\\}" -f $Color)
# Split the color into background and foreground colors. The
# \\[Math\\]::Truncate method returns a \\[Double\\], so cast it to an \\[Int\\].
$bg = \\[Int\\] \\[Math\\]::Truncate($newcolor / 0x10)
$fg = $newcolor -band 0xF

# If the background and foreground colors match, throw an error;
# otherwise, set the colors.
if ($bg -eq $fg) \\{
  Write-Error "The background and foreground colors must not match."
\\} else \\{
  $HOST.UI.RawUI.BackgroundColor = $bg
  $HOST.UI.RawUI.ForegroundColor = $fg
\\}

This script requires a single parameter: A two-digit hex value representing the new colors you want to use. Set-Color.ps1's parameter is identical to the parameter used with Cmd.exe's Color command: The first hex digit is the background color, and the second hex digit is the text color. For example, the command

Set-Color 9F

changes the console's colors to white text on a dark blue background. If the parameter you specify isn't valid or ifyou set the text and background to the same color, Set-Color.ps1 generates an error.

You can combine the scripts to easily store the current screen colors, change to new colors, and restore the screen colors. For example, consider the code

$colors = Get-Color
Set-Color 4F
Remove-Item $ENV:Temp\* -confirm
Set-Color $colors

The first line retrieves the current colors using Get-Color.ps1. The second line changes the colors to white text on a dark red background (4F). The third line, which executes the Remove-Item cmdlet to delete everything in your Temp folder to free up disk space, will appear in the new colors. The last line restores the console's original colors.

You can download Get-Color.ps1, Set-Color.ps1, as well as the script that created Figure 1 (EnumColors.ps1) by clicking the Download the Code Here button near the top of the page. If you're unfamiliar with how to run PowerShell scripts, see the article "Running PowerShell Scripts Is as Easy as 1-2-3."