MadBoy
MadBoy

Reputation: 11104

Muliple Foreground Colors in Powershell in One Command including spaces

I wanted to make a nice GUI for my little script. I've this post describing a function how to do it.

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
    for ($i = 0; $i -lt $Text.Length; $i++) {
        Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
    }
    Write-Host
}

Write-Color -Text Red,White,Blue -Color Red,White,Blue

It generally works fine but it doesn't like when there are spaces. I wanted to make sure the output is good looking so I've added some spaces..

enter image description here

Write-Color -Text "`t`t`SQL Connectivity:   ", "Verified ($gSerlServer\$gServerSqlDB)" -Color White, Green

Is there a way so that it would include the spaces and not act weirdly (at least from my point of view?). The funny thing is it sometimes display it correctly, and sometimes it doesn't. I don't have a clue why?

Upvotes: 2

Views: 372

Answers (1)

MadBoy
MadBoy

Reputation: 11104

I've no idea why this works but adding Start-Sleep before executing colors solves the problem

Start-Sleep -Milliseconds 30
# Write-Color goes here... 

So for every new display after clearing screen (going thru menus) I actually set the Start-Sleep. It's like execution is too fast with display before colors catch up the the screen.

Upvotes: 1

Related Questions