Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Color My World

Posted on September 3, 2010March 28, 2011

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: Color My World”

  1. Pingback: Tweets that mention Friday Fun: Color My World | The Lonely Administrator -- Topsy.com

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d