Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active May 18, 2024 21:16
Show Gist options
  • Save markwragg/187624b3e68ce699fceedf06008d9095 to your computer and use it in GitHub Desktop.
Save markwragg/187624b3e68ce699fceedf06008d9095 to your computer and use it in GitHub Desktop.
A PowerShell function to output text to screen as if typed by a human. I don't know why I wrote this.
Function Write-Human {
<#
.SYNOPSIS
Use to output text as if typed by a human.
.SYNOPSIS
This script takes one or more strings and prints them to the screen with a slightly randomised delay between each character to emulate a human typing.
.EXAMPLE
Get-Content .\mytestfile.txt | Write-Human
#>
[cmdletbinding()]
Param(
[parameter(ValueFromPipeline)]
[string]
$InputObject,
[int]
$Speed = 1,
[ValidateSet('Disco','DiscoWord', 'Terminal')]
[string]
$Mode
)
Begin {
if ($Mode -eq 'Terminal') {
$console = $host.ui.rawui
$console.backgroundcolor = "black"
Clear-Host
}
}
Process {
$InputObject -Split "`n" | ForEach-Object {
$FGColor = (7..15 | Get-Random)
ForEach ($Char in $_.ToCharArray()) {
Switch ($Mode) {
'Disco' { Write-Host $Char -NoNewLine -ForegroundColor (7..15 | Get-Random) }
'DiscoWord' {
Write-Host $Char -NoNewLine -ForegroundColor $FGColor
if ($Char -eq ' ') { $FGColor = (7..15 | Get-Random) }
}
'Terminal' { Write-Host $Char -NoNewLine -ForegroundColor Green -BackgroundColor Black }
default { Write-Host $Char -NoNewLine }
}
Start-Sleep -Milliseconds (Get-Random -Min (10 / $Speed) -Max (100 / $Speed))
}
Write-Host
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment