Is there a way to center text in PowerShell

前端 未结 3 1407
长发绾君心
长发绾君心 2021-01-14 23:46

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

3条回答
  •  Happy的楠姐
    2021-01-14 23:59

    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 '*****'
    

提交回复
热议问题