How to Use PowerShell Read-Host with Default Values?

In this tutorial, I will explain how to use the Read-Host cmdlet in PowerShell to prompt for user input with a default value. As a developer, I often encounter situations where I need to prompt users for input but also want to provide a sensible default value.

To use a default value with Read-Host in PowerShell, prompt the user and check if the input is empty. If it is, assign the default value. For example:

$department = Read-Host "Enter the department name (default is Sales)"
if ([string]::IsNullOrEmpty($department)) {
    $department = "Sales"
}

This method ensures that “Sales” is used as the default if no input is provided.

PowerShell Read-Host With Default Values

The Read-Host cmdlet in PowerShell prompts the user for input during script execution. It reads a line of input from the console and returns it as a string. However, Read-Host does not natively support default values. However, you can follow the methods below to use default values with the PowerShell Read-Host cmdlet.

Method 1: Using a Simple If Statement

One way to provide a default value in Read-Host cmdlet is by using an if statement to check if the user input is empty. If it is, we assign the default value.

Here is an example.

$department = Read-Host "Enter the department name (default is Sales)"
if ([string]::IsNullOrEmpty($department)) {
    $department = "Sales"
}
Write-Output "Department: $department"

In this example, if the user presses Enter without typing anything, the $department variable is set to “Sales”.

Here is the exact output in the screenshot below:

powershell read-host default value

Check out PowerShell Get-Process

Method 2: Using an Array and Filtering

Another method involves using an array to store the user input and the default value, then selecting the first non-empty value. Here is an example.

$department = @(Read-Host "Enter the department name (default is Sales)"; "Sales") | Where-Object { $_ -ne "" } | Select-Object -First 1
Write-Output "Department: $department"

This approach filters out empty values and selects the first non-empty value, effectively using “Sales” as the default if no input is provided.

Here is the exact output in the screenshot below:

powershell read-host default value examples

Method 3: Using a Function

Creating a custom function can encapsulate the logic for reusability and cleaner code. Here is how to use a function to use PowerShell Read-Host with Default Values.

function Read-HostWithDefault {
    param (
        [string]$Prompt,
        [string]$Default
    )
    $input = Read-Host $Prompt
    if ([string]::IsNullOrEmpty($input)) {
        return $Default
    } else {
        return $input
    }
}

$department = Read-HostWithDefault -Prompt "Enter the department name (default is Sales)" -Default "Sales"
Write-Output "Department: $department"

This function Read-HostWithDefault takes a prompt and a default value as parameters, making it versatile for various inputs.

Here is the exact output in the screenshot below:

PowerShell Read-Host with Default Values

Check out PowerShell Compare-Object

PowerShell Read-Host With Default Value Example

Suppose you are automating a script for setting up user profiles in a New York office. If the user doesn’t specify one, you need to prompt for the user’s role but want “Employee” as the default role. Using the methods above, you can ensure your script runs smoothly without unnecessary interruptions.

$role = Read-HostWithDefault -Prompt "Enter the user role (default is Employee)" -Default "Employee"
Write-Output "User Role: $role"

By implementing these techniques, you can enhance your PowerShell scripts, making them more user-friendly and efficient.

Conclusion

In this tutorial, I explained how to use PowerShell Read-Host with Default Values using various methods, such as simple conditionals, arrays, or custom functions. I have also explained a real example of the PowerShell Read-Host with default values.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.