interactive icon indicating copy to clipboard operation
interactive copied to clipboard

Read-Host returns 'Input request cancelled' when hitting Enter key without typing any input

Open rohancragg opened this issue 1 year ago • 0 comments

The following snippet in a Powershell Notebook cell reports an error if hitting the [Enter] key when prompted and does not behave as it would in a Powershell terminal (i.e. to return the value 'DEFAULT' without reporting an error):

$value = 'DEFAULT'
$value = if ($_ = Read-Host -Prompt "Enter value, or [Enter] to use default value - '$value'") {$_} else {$value}
$value

The error is

Read-Host: Line | 2 | … = if ($_ = Read-Host -Prompt "Enter value, or [Enter] to use default … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Input request cancelled DEFAULT

The code is as suggested in this answer on StackOverflow

  • OS
    • [X] Windows 11
  • Browser
    • [X] Edge
  • Frontend
    • [X] Visual Studio Code

rohancragg avatar Mar 20 '24 12:03 rohancragg