question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Read-Host` should have a `-TimeoutSecond` parameter

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 months ago
  • Reactions:2
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rhubarb-geek-nzcommented, May 18, 2023

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.

#!/usr/bin/env pwsh

Import-Module PSReadLine

$runspace=[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace

$cancellationTokenSource = New-Object -Type System.Threading.CancellationTokenSource
$cancellationTokenSource.CancelAfter(5000)
$cancellationToken = $cancellationTokenSource.Token
try
{
	$line = [Microsoft.PowerShell.PSConsoleReadLine]::ReadLine($runspace,$executionContext,$cancellationToken,$null)

	$line
}
finally
{
	$cancellationTokenSource.Dispose()
}

So even though the five seconds has elapsed, the ReadLine does not return until a key has been pressed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure a timeout for Read-Host in PowerShell
The idea seems to be to check whenever the user pressed a key using $Host.UI.RawUI.KeyAvailable and check that for the duration of your...
Read more >
add 'TimeOut' and 'DefaultValue' parameter to read-host
Summary of the new feature / enhancement Pwsh has no timeout. ... Read-Host should have a -TimeoutSecond parameter #19664.
Read more >
[SOLVED] Function in powertshell - PowerShell
I was thinking it would be good to have the following on a function and set the $value_input to 0 if the right...
Read more >
PowerShell: Read-Host with Timeout - The Curious Geek
When I use the -join operator to turn the char array into a string, it processes the backspaces and the resultant string does...
Read more >
Wait-Event (Microsoft.PowerShell.Utility)
The Wait-Event cmdlet suspends execution of a script or function until a particular event is raised. Execution resumes when the event is detected....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found