4

How can we center text in PowerShell? WindowWidth doesn't exist apparently, so is there a way somehow to keep the text centered?

We want this output :

    *
   ***
  *****
 *******

So I wrote

for ($i=1; $i -le 7; $i+=2)
{
  write-host("*" * $i)
}

But we get

*
***
*****
*******
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Didix
  • 567
  • 1
  • 7
  • 26

3 Answers3

7
function Write-HostCenter { param($Message) Write-Host ("{0}{1}" -f (' ' * (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Floor($Message.Length / 2)))), $Message) }

Write-HostCenter '*'
Write-HostCenter '***'
Write-HostCenter '*****'
fdafadf
  • 809
  • 5
  • 13
4

I had a bit of fun and wrote some code based on this, that makes a box and center the text inside. Im sure someone can make a cleaner version, but this do the job just fine :)

# ----------------------------------------------------------------------------------
#
# Script functions
#
# ----------------------------------------------------------------------------------
function MakeTopAndButtom
{
    $string = "# "
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + "-"
    }
    $string = $string + " #"

    return $string
}

function MakeSpaces
{
    $string = "# "
    for($i = 0; $i -lt $Host.UI.RawUI.BufferSize.Width - 4; $i++)
    {
        $string = $string + " "
    }
    $string = $string + " #"
    return $string
}

function CenterText
{
    param($Message)

    $string = "# "

    for($i = 0; $i -lt (([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 4; $i++)
    {
        $string = $string + " "
    }

    $string = $string + $Message

    for($i = 0; $i -lt ($Host.UI.RawUI.BufferSize.Width - ((([Math]::Max(0, $Host.UI.RawUI.BufferSize.Width / 2) - [Math]::Max(0, $Message.Length / 2))) - 2 + $Message.Length)) - 2; $i++)
    {
        $string = $string + " "
    }

    $string = $string + " #"
    return $string

}

function LinesOfCodeInCorrentFolder
{
    return (gci -include *.ps1 -recurse | select-string .).Count
}

$MakeTopAndButtom = MakeTopAndButtom
$MakeSpaces = MakeSpaces
$lines = LinesOfCodeInCorrentFolder



# ----------------------------------------------------------------------------------
#
# Run
#
# ----------------------------------------------------------------------------------
$MakeTopAndButtom
$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeSpaces

CenterText "Lines of .ps1 code in this folder: $($lines)"
CenterText "Press any key to exit"

$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeSpaces
$MakeTopAndButtom

Read-Host

This gives an output like this:

# ---------------------------------------------------------------------------------------- #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                       Lines of .ps1 code in this folder: 6524                            #
#                                 Press any key to exit                                    #
#                                                                                          #
#                                                                                          #
#                                                                                          #
#                                                                                          #
# ---------------------------------------------------------------------------------------- #
Mikki Sørensen
  • 101
  • 1
  • 9
3

You can calculate the spaces you need to add and then include them as follows:

$Width = 3

for ($i=1; $i -le 7; $i+=2)
{   
    Write-Host (' ' * ($width - [math]::floor($i / 2))) ('*' * $i)
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68