Author Posts

January 17, 2017 at 10:56 pm

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

$String

in Green?

Thank you,

Tony

January 17, 2017 at 11:02 pm

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 :).

January 18, 2017 at 7:24 am

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

March 9, 2018 at 5:16 pm

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.