PowerTip: Prompt user for value but provide default

Summary: Cloud & Datacenter Management MVP, Thomas Rayner, shows how to set a variable to a default value in PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to prompt a user for a value for a variable, but provide a default value if the user doesn’t enter anything?

Hey, Scripting Guy! Answer First prompt the user for the value to the variable and then detect if the response was empty. If the response is empty, set the value of the variable. You can validate parameters for a function more conveniently than this. This code is for inline variables in an interactive script:

$Interesting = Read-Host -Prompt 'Give me a value [some-value]'

if ([string]::IsNullOrWhiteSpace($Interesting))


$Interesting = 'some-value'


Comments (1)

  1. Vector BCO says:

    It’s really quick method.
    I try compare this method with ($Interesting -eq $null) -or ($Interesting -eq “”) and $Interesting -match ‘^\s*$’
    “-match” slowest variant, but I don`t understand why

    I use

    $Interesting = ‘ ‘
    # [string]::IsNullOrWhiteSpace($Interesting)
    # ($Interesting -eq $null) -or $Interesting -eq “”
    # $Interesting -match ‘^\s+$’
    for ($i = 0; $i -lt 1000000; $i++ ){

    for measure results

Skip to main content