How to Use PowerShell Read-Host?

In this tutorial, I will explain how to use the PowerShell Read-Host cmdlet with syntax and examples. As a developer, I once faced a situation where I needed to gather user input to automate the creation of user accounts in Active Directory. This tutorial will help you understand how to use Read-Host to prompt users for input, ensuring your scripts are interactive and user-friendly.

To use the PowerShell Read-Host cmdlet, you can prompt users for input by specifying a prompt message using the -Prompt parameter. For example, Read-Host -Prompt "Enter your name" will display the message “Enter your name” and wait for user input. This cmdlet can also capture sensitive information securely by using the -AsSecureString parameter, ensuring that input like passwords are masked.

What is PowerShell Read-Host?

The Read-Host cmdlet in PowerShell is used to read a line of input from the console. It is particularly useful when you need to prompt the user for information, such as entering a username, password, or any other required data. This cmdlet can also be used to mask sensitive information like passwords.

Syntax of Read-Host

The basic syntax for Read-Host is as follows:

Read-Host [-Prompt] <string> [-AsSecureString]
  • -Prompt: This parameter specifies the text to display as a prompt to the user.
  • -AsSecureString: This parameter ensures that the input is masked, making it suitable for sensitive information like passwords.

Check out PowerShell Get-Process

PowerShell Read-Host Examples

Now, let me show you some examples of PowerShell Read-Host.

Example 1: Prompt for a Username

Let’s start with a simple example where we prompt the user for their name:

$name = Read-Host -Prompt "Please enter your name"
Write-Host "Hello, $name! Welcome to our system."

In this example, the script prompts the user to enter their name and then greets them with a personalized message.

I executed the above PowerShell script code using VS code, and you can see the exact output in the screenshot below:

PowerShell read-host

Check out PowerShell Compare-Object

Example 2: Validate User Input

To ensure the user provides valid input, you can add a validation loop. For instance, if you need the user to enter a number between 1 and 99:

do {
    $number = Read-Host "Enter a number from 1 to 99"
} while ($number -lt 1 -or $number -gt 99)
Write-Output "You entered a valid number: $number"

In this script, the loop continues to prompt the user until they enter a number within the specified range.

Here is the exact output in the screenshot below:

PowerShell read-host examples

Example 3: Multiple Prompts in a Script

You can use Read-Host multiple times in a script to gather different pieces of information. For example, if you need the user’s first name, last name, and email address:

$firstName = Read-Host "Enter your first name"
$lastName = Read-Host "Enter your last name"
$email = Read-Host "Enter your email address"
Write-Output "User Info: $firstName $lastName, Email: $email"

This script collects the user’s first name, last name, and email address, and then outputs the collected information.

Check out PowerShell Export-CSV cmdlet

Example 4: Gather User Information for Account Creation

Imagine you are setting up new user accounts and need to gather information such as the user’s first name, last name, and email address. Here’s how you can use Read-Host to achieve this:

$firstName = Read-Host -Prompt "Enter the user's first name"
$lastName = Read-Host -Prompt "Enter the user's last name"
$email = Read-Host -Prompt "Enter the user's email address"

Write-Host "Creating account for $firstName $lastName with email $email..."
# Add your account creation logic here

This script prompts the user for essential information needed to create an account and then proceeds with the account creation process.

Example 5: Secure Password Input

When dealing with user credentials, it’s crucial to handle passwords securely. The -AsSecureString parameter allows you to mask the input:

$password = Read-Host -Prompt "Enter your password" -AsSecureString
Write-Host "Password has been securely captured."

In this example, the user’s password is captured securely, preventing it from being displayed in plain text.

Here is the exact output in the screenshot below:

How to Use PowerShell Read-Host

Check out PowerShell Set-Content cmdlet

Example 6: Automate Active Directory User Lookup

Suppose you need to automate the process of checking if a user account is locked out in Active Directory. You can use Read-Host to prompt for the username and then perform the lookup.

Here is the complete PowerShell script.

$username = Read-Host -Prompt "Enter the username to check"
# Assuming Get-ADUser is available and configured
$user = Get-ADUser -Identity $username -Properties LockedOut
if ($user.LockedOut) {
    Write-Host "The account for $username is locked out."
} else {
    Write-Host "The account for $username is not locked out."
}

This script prompts the user for a username and checks if the account is locked out in Active Directory.

Conclusion

In this tutorial, I explained how to use the Read-Host cmdlet in PowerShell to prompt users and capture their input. We can use Read-Host cmdlet to gather user input for simple tasks or secure credentials for sensitive operations. We also saw a few examples of using the PowerShell Read-Host cmdlet.

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.