center text

Welcome Forums General PowerShell Q&A center text

This topic contains 4 replies, has 4 voices, and was last updated by

1 year, 5 months ago.

  • Author
  • #62064

    Topics: 74
    Replies: 167
    Points: 83
    Rank: Member

    Is there a way to center text horizontally?

    Also, let's say I have

    $String = "Some String"

    Instead of

    Write-Host $String -foregroundcolor "Green"

    to display the text in Green,

    How can I display


    in Green?

    Thank you,


  • #62067

    Topics: 13
    Replies: 4872
    Points: 1,843
    Helping HandTeam Member
    Rank: Community Hero

    Write-Host is the only way to use colors, because it draws pixels directly on the screen rather than passing objects through a pipeline. And, because PowerShell's main use cases weren't presenting formatted text on screen, there's no built-in way of telling Write-Host to center that text. You'd have to dig around in the $host object, I expect, figure out the current window width in character, and calculate your own padding to display something centered.

    Creating user interfaces has always been time-consuming and not very much fun :).

  • #62071

    Topics: 0
    Replies: 11
    Points: 1
    Rank: Member

    The center option is interesting & where you might need to get creative (I can enjoy these challenges). I have a script which displays a number that will be 1-3 characters in length at the beginning of a line, as there is additional text after these numbers I have $pad1, $pad2 & $pad3 variables that will add 1, 2 or 3 spaces after the number so the total character count is always 4 and the extra text is aligned properly.

    If you know how the text is going to appear on screen every time you could create a variable with enough spaces to center the text. Mathematically, the screen is x characters across, if you count the amount of characters (y) in the text you wish to center you could then take (x – y) / 2 = # of characters you need to move the text in to center it.

    80 characters wide, 20 characters of text, (80 – 20) / 2 = 30 spaces required to indent text

    As you can't have 1/2 a character, you could possibly assume that you will round the number up to the nearest whole number, so you could use something like this: "{0:N0}" -f ((80-19)/2) to get 31.

    Finally and I only found this out by doing web searches just now you can use padleft to add the spaces for you.

    $a = "This is text"
    $a.PadLeft( "{0:N0}" -f ((80 – ($a | measure).count)/2)," ")

    I have a few things going on but basically we are using normal .padleft format which is:
    $a.padleft(# of characters, character to insert) i.e. $a.padleft(40," ")

    Within the () you have: "{0:N0}" -f – a digit with no decimal places, then 80 – the character count of $a and dividing in by 2 on the fly and PowerShell rounds it up for us.

    As to the color question, the following works in PSv5 on Windows 10:

    $test = "Write-host 'this is a test in green' -fore green"
    Write-host "this is a test in white and " -nonewline ; invoke-expression $test

  • #62079

    Topics: 74
    Replies: 167
    Points: 83
    Rank: Member

    Thank you all

  • #95579

    Topics: 0
    Replies: 1
    Points: 0
    Rank: Member

    I signed up here simply to share this.

    I took the idea of James Bernie and made it a reality.

    Center Write-Host Output

    I managed to AD-hoc PolyMorphic Power Time the ability to use Text Color, However someone would need to take this source code (a function) and add another overloading functions to support background color, as well as both background and foreground colors.

    I didn't go that deep into it, but its a solid start.

The topic ‘center text’ is closed to new replies.