Set the Default Value With Read-Host in PowerShell

  1. Overview of Read-Host in PowerShell
  2. Different Ways to Set the Default Value of the Prompt With Read-Host in PowerShell

In this article, we shall understand different ways to set the default value for the Read-Host command.

Install Windows SSH client PowerShe...
Install Windows SSH client PowerShell

Overview of Read-Host in PowerShell

It performs a line-by-line reading of the input from the console. An input line is taken from the console and read by the Read-Host cmdlet (stdin).

You may use it to ask a user for feedback in many situations. This cmdlet enables you to ask users for sensitive data like passwords since it allows you to preserve the input as a secure string.

Read-Host limits the number of 1022 characters, to be exact, that it will accept as input from a user. Following is the syntax of Read-Host to take input and store it in a variable.

$Name = Read-Host "Please enter your name."

The $Name variable is updated with the inputted value whenever a value is entered, and the Enter key is hit.

Different Ways to Set the Default Value of the Prompt With Read-Host in PowerShell

First Approach to Set the Default Value

Create a variable called defaultPromptValue and give it the string value Saad. This value will be used in place of the user’s input if they choose not to provide one.

$defaultPromptValue = 'Saad'

Define a new variable, promptOutput, that will produce a message asking the user for input and will utilize the Read-Host command to do this action.

$promptOutput = Read-Host "Press space to use default value [$($defaultPromptValue)]"

The next step is to implement a condition that checks to see whether the user has provided any input. If the answer is yes, the entered value will be assigned to the defaultPromptValue variable.

When writing the condition for this operation, we have two different choices.

if ($promptOutput -eq "") {} else {
    $defaultPromptValue = $promptOutput
}

OR

if (!$promptOutput -eq "") {$defaultPromptValue = $promptOutput}

Code:

$defaultPromptValue = 'Saad'
$promptOutput = Read-Host "Press space to use default value [$($defaultPromptValue)]"
if (!$promptOutput -eq "") {$defaultPromptValue = $promptOutput}

Another Approach to Set the Default Value

In this approach, we relocate the process of receiving user input inside the condition that checks whether the user has entered any value or not. This allows us to better track whether or not the user has provided any data.

Without any action from the user, we use the default value, Saad. If the user offers input, however, we use the value saved in the promptOutput variable.

Code:

$defaultPromptValue = 'Saad'
if (($promptOutput = Read-Host "Press enter to accept default value $defaultPromptValue") -eq '') {$defaultPromptValue} else {$promptOutput}

Last Approach to Set the Default Value

This is the last but certainly not the least important method to set the default value. It verifies that the input is given from the user, and if it is, it then assigns the value of the user input straight to the variable named defaultPromptValue.

Code:

$defaultPromptValue = 'Saad'
if ($promptOutput = Read-Host "Value [$defaultPromptValue]") { $defaultPromptValue = $promptOutput }

This way, it does not have to use the else condition.