Skip to content

Latest commit

 

History

History
97 lines (81 loc) · 2.47 KB

File metadata and controls

97 lines (81 loc) · 2.47 KB

The 'write-animated.ps1' Script

This PowerShell script writes text centered and animated to the console.

Parameters

PS> ./write-animated.ps1 [[-text] <String>] [[-speed] <Int32>] [<CommonParameters>]

-text <String>
    Specifies the text line to write ("Welcome to PowerShell" by default)
    
    Required?                    false
    Position?                    1
    Default value                Welcome to PowerShell
    Accept pipeline input?       false
    Aliases                      
    Accept wildcard characters?  false

-speed <Int32>
    Specifies the animation speed per character (10ms by default)
    
    Required?                    false
    Position?                    2
    Default value                10
    Accept pipeline input?       false
    Aliases                      
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./write-animated.ps1
(watch and enjoy)

Notes

Author: Markus Fleschutz | License: CC0

Related Links

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Writes animated text
.DESCRIPTION
	This PowerShell script writes text centered and animated to the console.
.PARAMETER text
	Specifies the text line to write ("Welcome to PowerShell" by default)
.PARAMETER speed
	Specifies the animation speed per character (10ms by default)
.EXAMPLE
	PS> ./write-animated.ps1
	(watch and enjoy)
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$text = "Welcome to PowerShell", [int]$speed = 10) # 10ms

function WriteLine([string]$line) {
	[int]$end = $line.Length
	$startPos = $HOST.UI.RawUI.CursorPosition
	$spaces = "                                                                     "
	[int]$termHalfWidth = 120 / 2
	foreach($pos in 1 .. $end) {
		$HOST.UI.RawUI.CursorPosition = $startPos
		Write-Host "$($spaces.Substring(0, $termHalfWidth - $pos / 2) + $line.Substring(0, $pos))" -noNewline
		Start-Sleep -milliseconds $speed
	}
	Write-Host ""
}

try {
	WriteLine $text 
	exit 0 # success
} catch {
	"⚠️ ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
        exit 1
}

(page generated by convert-ps2md.ps1 as of 03/27/2026 07:17:23)