Skip to content

Instantly share code, notes, and snippets.

@nathanchere
Last active February 26, 2020 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanchere/704920a4a43f06f4f0d2 to your computer and use it in GitHub Desktop.
Save nathanchere/704920a4a43f06f4f0d2 to your computer and use it in GitHub Desktop.
Powershell: timed input example
Function TimedPrompt($prompt,$secondsToWait){
Write-Host -NoNewline $prompt
$secondsCounter = 0
$subCounter = 0
While ( (!$host.ui.rawui.KeyAvailable) -and ($count -lt $secondsToWait) ){
start-sleep -m 10
$subCounter = $subCounter + 10
if($subCounter -eq 1000)
{
$secondsCounter++
$subCounter = 0
Write-Host -NoNewline "."
}
If ($secondsCounter -eq $secondsToWait) {
Write-Host "`r`n"
return $false;
}
}
Write-Host "`r`n"
return $true;
}
$val = TimedInput "Press key to skip restore; will begin in 3 seconds" 3
Write-Host $val
@trevren11
Copy link

I tried this on powershell 5.1 and it doesn't work. Also you need to make TimedInput match TimedPrompt

@diegojancic
Copy link

This solution works in v5.1:
https://stackoverflow.com/a/52546471/72350

@rpresser
Copy link

rpresser commented Feb 26, 2020

This solution works in v5.1:
https://stackoverflow.com/a/52546471/72350

No, it does not.

Exception calling "FlushInputBuffer" with "0" argument(s): "The method or operation is not implemented."

Running PSVersion 5.1.17763.771

EDIT: Ah, the exception only happens in PowerShell ISE. In a straight PS window it's fine. It appears raw UI can't be checked at all in ISE, so beware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment