PoshCode Archive  Artifact [1a505ebaa2]

Artifact 1a505ebaa2144dab0872a053579f8d56cf63e3f826b10f80e5d913b3a4f31512:

  • File Write-Typewriter.ps1 — part of check-in [37b4f24968] at 2018-06-10 14:10:02 on branch trunk — Make write-host text appear as if it is being typed on a typewriter (user: Nathan Kasco size: 1365)

# encoding: ascii
# api: powershell
# title: Write-Typewriter
# description: Make write-host text appear as if it is being typed on a typewriter
# version: 1.0
# type: function
# author: Nathan Kasco
# license: CC0
# function: Write-Typewriter
# x-poshcode-id: 6193
# x-derived-from-id: 6194
# x-archived: 2016-05-17T02:44:44
# x-published: 2016-01-26T00:27:00
#
#
#Typewriter Text
function Write-Typewriter
{
<#
.Synopsis
   Make write-host text appear as if it is being typed on a typewriter
.DESCRIPTION
   Input text and if desired specify the write speed (25-250 milliseconds) for the text
.EXAMPLE
   Write-Typewriter "Hello world!"
.EXAMPLE
   Write-Typewriter "Hello world!" 250
.NOTES
   v1.0 - 2016-01-25 - Nathan Kasco
#>

    [CmdletBinding()]
    [Alias()]
    [OutputType([string])]

    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [alias("Name")]
        [string]
        $text,
        
        [Parameter(Mandatory=$false, Position=1)]
        [ValidateRange(25,250)]
        [int]
        $typeSpeed = 125
    )

    function sleep-host{
        Start-Sleep -Milliseconds $typeSpeed
    }

    #Cycle through letters
    $count = 0
    while($count -lt $text.Length){
        Write-Host $text.Chars($count) -NoNewline
        sleep-host
        $count++
    }
}