Color My PowerShell World
In this issue:
I feel I've spent time over the last few months with articles and code samples that offer ways to stylize PowerShell output with color. You might encounter other tools that provide color formatting options, and they might have specific requirements. I thought I would spend some time exploring the various ways to apply color in PowerShell, including some of the built-in options and how you might convert color values from one format to another.
Console Colors
The most basic color option is associated with the console host. You've seen this countless times with Write-Host which supports foreground and background colors. These colors are derived from the [System.ConsoleColor] .NET class.
PS C:\> Get-TypeMember System.ConsoleColor
Type: System.ConsoleColor
Name MemberType ResultType IsStatic IsEnum
---- ---------- ---------- -------- ------
Black Field System.ConsoleColor True
Blue Field System.ConsoleColor True
Cyan Field System.ConsoleColor True
DarkBlue Field System.ConsoleColor True
DarkCyan Field System.ConsoleColor True
DarkGray Field System.ConsoleColor True
DarkGreen Field System.ConsoleColor True
DarkMagenta Field System.ConsoleColor True
DarkRed Field System.ConsoleColor True
DarkYellow Field System.ConsoleColor True
Gray Field System.ConsoleColor True
Green Field System.ConsoleColor True
Magenta Field System.ConsoleColor True
Red Field System.ConsoleColor True
White Field System.ConsoleColor True
Yellow Field System.ConsoleColor True
GetType Method Type
HasFlag Method Boolean
ToString Method String
> I am using the Get-TypeMember function from the PSScriptTools module.
The Field member type indicates these are static values.
PS C:\> [consolecolor]::Red
Red
PS C:\> [consolecolor]::Red | Select *
value__
-------
12
That value__ property is the integer value associated with the color which suggests that these values are actually enumerated values.

You can enumerate the available colors with a simple foreach statement.
[enum]::GetValues([System.ConsoleColor]).foreach({$m = "{0} = {1}" -f $_,$_.Value__; Write-Host $m -fore $_})

Under the hood, PowerShell uses these values with an internal methods called WriteLine() and Write().
PS C:\> $host.ui.WriteLine.OverloadDefinitions
void WriteLine()
void WriteLine(string value)
void WriteLine(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor, string value)
PS C:\> $host.ui.Write.OverloadDefinitions
void Write(string value)
void Write(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor, string value)
Although, you should stick to using the Write-Host cmdlet.
Scripting with Console Colors
When it comes to scripting, because the [ConsoleColor] values are enumerated, you can get automatic tab completion. Here's a simple reference function.
Function Write-Something {
[CmdletBinding()]
Param(
[Parameter(Position = 0, Mandatory)]VC
[ValidateNotNullOrEmpty()]
[string]$Message,
[ValidateNotNullOrEmpty()]
[string]$Prefix = "[$((Get-Date).TimeOfDay)]",
[ConsoleColor]$Color = "White"
)
$msg = "{0} {1}" -f $Prefix,$Message
Write-Host $msg -ForegroundColor $Color
}

I don't have to do anything special to get tab completion for the $Color parameter. PowerShell automatically recognizes the [ConsoleColor] type.
