I want to output many different foreground colors with one statement.
PS C:\\> Write-Host \"Red\" -ForegroundColor Red
Red
This output i
This code is available with a different number of arguments: Text, ForeGroundColor, and BackGroundColor.
Each colorlist is used with a rotate implementation:
function Write-Color([String[]]$Text, [ConsoleColor[]]$ForeGroundColor, [ConsoleColor[]]$BackGroundColor) {
for ($i = 0; $i -lt $Text.Length; $i++) {
$Color = @{}
if ($ForeGroundColor -and $BackGroundColor){
$Color = @{
ForegroundColor = $ForeGroundColor[$i%($ForeGroundColor.count)]
BackgroundColor = $BackGroundColor[$i%($BackGroundColor.count)]
}
} elseif ($ForeGroundColor) {
$Color = @{
ForegroundColor = $ForeGroundColor[$i%($ForeGroundColor.count)]
}
} elseif ($BackGroundColor) {
$Color = @{
BackgroundColor = $BackGroundColor[$i%($BackGroundColor.count)]
}
}
Write-Host $Text[$i] @color -NoNewLine
}
Write-Host
}
Log usage:
Write-Color "Check color list...".PadRight(50), '[', ' OK ', ']' -fore cyan, White, green, white
Write-Color "Red Check is good...".PadRight(50), '[' ,' ERROR! ', ']' -fore cyan, White, red, white
Write-Color "Write-Color is cool !".PadRight(50), '[', ' WARN ', ']' -fore cyan, White, Yellow, white
List Usage (just 2 backGroundColor and 4 foreGroundColor):
Write-Color (@(100..115) | %{" -> $_ : ".PadRight(30) + "`n"}) -ForeGroundColor cyan, yellow, magenta, red -BackGroundColor gray, black
Standard Write-Host
Write-Host (@(100..115) | %{" -> $_ : ".PadRight(30) + "`n"}) -BackgroundColor gray