PowerShell CheatSheet – Give some colors to your output

To improve readability and to easily differentiate the items returned by the script to the user, the usage of color in the output is an essential feature.
So we have two parameters at our disposal:

  1. -BackgroundColor
  2. -ForegroundColor

And for each one we have 16 colors/shades available:

  1. Black
  2. Blue
  3. Cyan
  4. DarkBlue
  5. DarkCyan
  6. DarkGray
  7. DarkGreen
  8. DarkMagenta
  9. DarkRed
  10. DarkYellow
  11. Gray
  12. Green
  13. Magenta
  14. Red
  15. White
  16. Yellow

240 possibilities; yes, there’s no point to write in black on… black! (for example)
But I’m not a BackgroundColor fan… And I don’t like Magenta and Cyan too… Ok, am I a psycho? No problem, I’m good with 14 colors available!
Anyway, there’s a good point to give an example of nested loops with arrays.

$backgcolors="Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow"
$frontcolors="Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow"

ForEach ($backgcolor in $backgcolors) {
$frontcolors=$frontcolors -ne $backgcolor
ForEach ($frontcolor in $frontcolors) {
$frontcolors="Black","Blue","Cyan","DarkBlue","DarkCyan","DarkGray","DarkGreen","DarkMagenta","DarkRed","DarkYellow","Gray","Green","Magenta","Red","White","Yellow"
Write-Host "I'm writing in $frontcolor on $backgcolor" -ForegroundColor $frontcolor -BackgroundColor $backgcolor;
}}

 
Ok, and what the output looks like?
output in color
Waouh, we’ll need a really big screen to have all in one shot…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top