Skip to content

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

@rohancragg

Description

@rohancragg

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
    • Windows 11
  • Browser
    • Edge
  • Frontend
    • Visual Studio Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-PowerShellSpecific to PowerShellbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions