Added write-centered.ps1

This commit is contained in:
Markus Fleschutz 2024-04-21 17:23:21 +02:00
parent 6132292e2a
commit d9f5847aed
2 changed files with 37 additions and 3 deletions

View File

@ -0,0 +1,33 @@
<#
.SYNOPSIS
Writes text centered
.DESCRIPTION
This PowerShell script writes the given text centered to the console.
.PARAMETER text
Specifies the text to write
.EXAMPLE
PS> ./write-headline.ps1 "Hello World"
Hello World
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$text = "")
try {
if ($text -eq "") { $text = Read-Host "Enter the text to write" }
$ui = (Get-Host).ui
$rui = $ui.rawui
[int]$numSpaces = ($rui.MaxWindowSize.Width - $text.Length) / 2
[string]$spaces = ""
for ([int]$i = 0; $i -lt $numSpaces; $i++) { $spaces += " " }
Write-Host "$spaces$text"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

View File

@ -1,10 +1,10 @@
<#
.SYNOPSIS
Writes text as headline
Writes a headline
.DESCRIPTION
This PowerShell script writes the given text as a headline.
.PARAMETER text
Specifies the headline text
Specifies the text to write
.EXAMPLE
PS> ./write-headline.ps1 "Hello World"
@ -17,8 +17,9 @@
#>
param([string]$text = "")
try {
if ($text -eq "") { $text = Read-Host "Enter the headline text" }
if ($text -eq "") { $text = Read-Host "Enter the text to write" }
Write-Host "`n* $text *" -foregroundColor green
[int]$len = $text.Length