Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active October 3, 2023 14:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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