PowerShell: `Read-Host` should have a `-TimeoutSecond` parameter
Summary of the new feature / enhancement
If the user input isn’t strictly required and some default can be used, it may make sense to have Read-Host
have a timeout so that the script isn’t blocked indefinitely.
Proposed technical implementation details (optional)
Would need to show the timeout, default, and countdown.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 3
- Comments: 17 (6 by maintainers)
Just to tie in a few conceptually related issues:
Unfortunately the Read-Host Cmdlet does not respond to the PowerShell being stopped.
Although Microsoft.PowerShell.PSConsoleReadLine does implement a ReadLine that takes a CancellationToken it doesn’t work until a key has been entered.
So even though the five seconds has elapsed, the ReadLine does not return until a key has been pressed.
There should also be a parameter here: -default_value_on_timeout
That’s also a can of worms though. We only got away with that in PSES because of the
ReadKey
override hijinks we do with PSReadLine. We’d need to standardize that hack, otherwise the first bit of input after a timeout would be dropped.hi all guys: Great, this feature is commendable. As I currently do not have this feature, I implemented it myself using a script. Well, perhaps my implementation is not perfect yet. Welcome to reference and improve. https://gitee.com/chuanjiao10/kasini3000/blob/master/read-host+timeout_v2.1.ps1