The end of another work week and time for a little PowerShell fun. When I first started using PowerShell, I was fascinated by Write-Host and the ability to write colorized text to the console. Visions of ANSI art danced in my head, but I've moved on. Using colors with Write-Host is a great thing, and something I actually encourage. Because Write-Host doesn't write to the pipeline, it can be confusing. My recommendation is to use Write-Output, or its alias Write, to send objects to the pipeline. Use Write-Host to display messages to the person running the script or function and colorize it so you can distinguish it from pipelined output. But what colors can I use? What do they look like? What about the combination for foreground and background colors? This week's Friday Fun will help.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The color choices that Write-Host uses come from an enumeration of the .NET [System.ConsoleColors] class. If you know how to use .NET in PowerShell, you can get this enumeration yourself. Don't feel bad if this looks like programming.
[cc lang="PowerShell"]
PS S:\> [enum]::GetNames([system.consolecolor])
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
[/cc]
That's a good start. But what does DarkCyan look like? I put together a short script called Demo-Color.ps1.
[cc lang="PowerShell"]
Param(
[switch]$ListOnly,
[switch]$combo)
#enumerate color options for the current console
$colors=[System.Enum]::GetNames([System.ConsoleColor])
if ($ListOnly) {
#display a sorted list of colors
$colors | Sort
}
Else {
#Demo color samples
Clear-Host
if ($combo) { #cycle through all background and foreground combinations
for ($i=0;$i -lt $colors.count;$i++) {
for ($j=0;$j -lt $colors.count;$j++) {
if ($colors[$i] -ne $colors[$j]) {
$msg="{0} on {1}" -f $colors[$i],$colors[$j]
Write-Host $msg -foregroundcolor $colors[$i] -backgroundcolor $colors[$j]
} #if
} #for $j
} #for $i
} #if $combo
else {
#show the list of colors
for ($i=0;$i -lt $colors.count;$i++) {
Write-Host " $($colors[$i]) " -BackgroundColor $colors[$i]
} #for
} #else
} #else no $ListOnly
[/cc]
The script uses the same enumeration to get all available colors. If you run the script with the -ListOnly parameter, it will return a sorted list of color options.
[cc lang="PowerShell"]
PS S:\> .\demo-color -list
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
[/cc]
But the best way to use it is without any parameters. This will run through the list of colors, using a For loop and Write-Host to display each one.
[cc lang="PowerShell"]
for ($i=0;$i -lt $colors.count;$i++) {
Write-Host " $($colors[$i]) " -BackgroundColor $colors[$i]
} #for
[/cc]
But you might also be curious about color combinations. Use the -Combo switch and the script will cycle through every combination of foreground and background color using a set of nested For loops.
[cc lang="PowerShell"]
for ($i=0;$i -lt $colors.count;$i++) {
for ($j=0;$j -lt $colors.count;$j++) {
if ($colors[$i] -ne $colors[$j]) {
$msg="{0} on {1}" -f $colors[$i],$colors[$j]
Write-Host $msg -foregroundcolor $colors[$i] -backgroundcolor $colors[$j]
} #if
} #for $j
} #for $i
[/cc]
I'll let you run it yourself to see the results. What I like about this is that I can check color combinations and find something that works best in the console and ISE. Some colors just don't work in the ISE very well. Of course, if you've changed your console's background color, you'll get even different results.
Download demo-color.ps1.
1 thought on “Friday Fun: Color My World”
Comments are closed.