Last active
February 26, 2020 15:22
-
-
Save nathanchere/704920a4a43f06f4f0d2 to your computer and use it in GitHub Desktop.
Powershell: timed input example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This solution works in v5.1:
https://stackoverflow.com/a/52546471/72350
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
I tried this on powershell 5.1 and it doesn't work. Also you need to make TimedInput match TimedPrompt