Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to center text in PowerShell

Tags:

powershell

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

*
***
*****
*******
like image 279
DIDIx13 Avatar asked Aug 30 '25 17:08

DIDIx13


1 Answers

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 '*****'
like image 114
fdafadf Avatar answered Sep 02 '25 20:09

fdafadf