How to simulate the human input with POWERSHELL

Hi everyone,

I want to simulate the human input with POWERSHELL, below is a sample code, I want to see the animation effect in I run the code, and gif but it’s still not good enough.

Any good idea? convert string to an array of letter, then print each letter? how about the delay of input, how to get a random delay for each letter?

$string = "This is a sample string."

# Split the string into an array of words.
$words = $string.Split()

# Iterate over the array and print each word.
foreach ($word in $words) {
    Write-Host -NoNewLine "$word " -ForegroundColor Blue
    Start-Sleep -Milliseconds 100

# Print a newline at the end.



I change it to below code, but I don’t understand why if statement never run even the boolean value is true, the $true argument isn’t pass to the function correctly? anyone can help to check?

function simulation($strings,$collors,$mind,$maxd,$clear,$run) {
    foreach ($string in $strings) {
        $characters = $string.ToCharArray()
        foreach ($character in $characters) {
            Write-Host -Verbose $character -NoNewLine -ForegroundColor $collors
            $delay = Get-Random -Minimum $mind -Maximum $maxd
            Start-Sleep -Milliseconds $delay
        if ($run){
            Write-Host "hello here"
$mystring =  @("moday","tuesday")
simulation $mystring "Red" 30 200 $true

$run is the 6th parameter.

You’re passing only 5 arguments to the function.

1 Like

I urgently recommend to use a param() block for your function definition and naming parameters when running functions.

I count 6 parameters but you only provide 5 of them…

And BTW: the parameter $clear isn’t even used in your function!! :smirk:

1 Like

thank you so much @Olaf