Is there a one-liner for using default values with Read-Host?

Shortest Version I could came up with:

if (!($value = Read-Host "Value [$default]")) { $value = $default }

This version doesn't have to use else.


$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($defaultValue)]"
$prompt = ($defaultValue,$prompt)[[bool]$prompt]

If you absolutely have to have it in one line:

$defaultValue = 'default'
($defaultValue,(Read-Host "Press enter to accept the default [$($defaultValue)]")) -match '\S' |% {$prompt = $_}

$DefaultValue="Foobar"

.... (Optional other code) ....

$Value=if($Value=(Read-Host "Enter value [$DefaultValue]")){$Value}else{$DefaultValue}

Just threw this together to re-use a previously entered config value while still allowing user to change it if needed... The accepted answer is missing the assignment portion and uses a hardcoded "Default" value...

There is a function (from other languages) called "Ternary Operator" or "Binary Operator"(https://en.wikipedia.org/wiki/%3F:) (the 'xx : yy ? zz' style one) and there are several people who have submitted functions to implement its behavior in Powershell.


if(($result = Read-Host "Press enter to accept default value [default]") -eq ''){"default"}else{$result}