Interactive PowerShell Scripts with Read-Host: A Complete Guide

Published:15 June 2019 - 2 min. read

Using the Read-Host PowerShell cmdlet, you can interactively prompt for input from the script user. Let’s see some real-world applications on how we can use the Read-Host PowerShell cmdlet.

Prompting for Input with Read-Host

The Read-Host cmdlet performs two functions in a PowerShell script; it pauses execution and receives input. That’s it. Read-Host is a simple cmdlet but one that comes in useful when needing to get information from the script user.

At it’s most basic, the Read-Host cmdlet simply requires using the Prompt parameter. This Prompt parameter allows you to give the script user some kind of indication as to what to input. For example, if your script requires a server name, you might choose to use Read-Host to prompt the user to input that when the script is run.

You can see below that by executing Read-Host within the PowerShell console itself using the Prompt parameter, PowerShell is stopping all execution and displays my prompt message giving the user an explanatory message as to what we’re after.

Read-Host PowerShell prompt
Read-Host PowerShell prompt

Let’s incorporate this into a script. Whenever a user inputs information at the prompt, Read-Host returns that information back to your code. You can easily capture this information by assigning the output to a variable. Perhaps I’d like to ask for a server name and then do something with that server name. Otherwise, I’d like to send a warning to the user letting them know that I really, really need that server name.

$serverName = Read-Host -Prompt 'Server name to process'
if ($serverName) {
    Write-Host "We can now use the server name [$serverName] in our code"
} else {
    Write-Warning -Message "No server name input."
}

Using a simple if/then construct, I can then be sure my user inputs a server name. Once they do, I can then capture it and do something with it otherwise, send a warning message.

Warning when no input
Warning when no input

Asking for Passwords

You should know it’s not a good idea to store passwords in plain text in your scripts. Likewise, it’s never a good idea to have passwords stored in plain text in memory either. To get around this, PowerShell has a concept called a secure string which is a simple string that’s encrypted.

A secure string can any kind of sensitive information; a password is a great example. What does this have to do with Read-Host? The Read-Host cmdlet has an AsSecureString parameter which allows the user to not only store the output as a secure string but also to show asterisk while typing to hide your secret from prying eyes!

Let’s say I have a deep, dark secret I don’t want anyone to know about but I need to pass this password to some kind of software. You can see below that when I don’t use AsSecureString, you’re onto me! However, if I use AsSecureString, my secret is safe since every character I type is replaced with an asterisk and the output it saved as a secure string rather than a plain-text string.

Accepting secure input with Read-Host
Accepting secure input with Read-Host

To reference the Read-Host documentation, check out the Microsoft Docs.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!