#StackBounty: #windows #powershell #batch-file #cmd #command Powershell MultiColored text on single line AND Centered

Bounty: 100

I have a powershell function that I need to be able to change. The function centers text in the terminal however, I need to be able to output multiple colors for text on a single line. If I do -NoNewLine and do more Write-host to change the color… then it still calculates the width of the terminal and still adds as much padding as it would without me adding -NoNewLine. Essentially I want my text centered and I want to be able to use multiple colors. With what I have I can only do 1 color per line.

function WriteCentered([String] $text, $color = $null)
{
    $width = [int](Get-Host).UI.RawUI.BufferSize.Width
    $twidth = [int]$text.Length
    $offset = ($width / 2) - ($twidth / 2)
    $newText = $text.PadLeft($offset + $twidth)

    if($color)
    {
        Write-Host $newText -ForegroundColor $color
    }        
    else
    {
        Write-Host $newText 
    }


}   

I have added more IF conditions, I have changed my padding calculations, I am having trouble with getting it just right.


Get this bounty!!!

Leave a Reply

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