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:
- -BackgroundColor
- -ForegroundColor
And for each one we have 16 colors/shades available:
- Black
- Blue
- Cyan
- DarkBlue
- DarkCyan
- DarkGray
- DarkGreen
- DarkMagenta
- DarkRed
- DarkYellow
- Gray
- Green
- Magenta
- Red
- White
- 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?
Waouh, we’ll need a really big screen to have all in one shot…