The Read-Host
cmdlet in PowerShell lets you prompt for user input, which can greatly enhance the functionality of your PowerShell scripts and make them more dynamic. Follow along as we learn all about the Read-Host
cmdlet in this article. Oh, and I hope you like examples because we got lots of them.
Why make your scripts interactive? For the same reason I interact with real life people: because sometimes we’re forced to. Just kidding — kind of. But for real, sometimes there’s just no way around it. To achieve certain results, user interaction is required. Thankfully, PowerShell makes it way easier to interact with users than doing it in real life.
What is the Read-Host PowerShell cmdlet?
The Read-Host
cmdlet in PowerShell accepts a line of input from the console. It’s one of the simplest commands to use with only a few optional parameters: -Prompt
, -AsSecureString
, and -MaskInput
. Let’s cover each parameter before we dive into the examples and use cases.
As always, I recommend reviewing the help documentation by running the command Get-Help Read-Host -full
, but we’ll cover most of the juicy details in this article.
Throughout this article I’m using PowerShell 7.4.4, not Windows PowerShell. While the two versions are similar, there are some key differences. For example, Windows PowerShell doesn’t support the -MaskInput parameter. Also, Windows PowerShell supports 8,190 characters of input, while PowerShell only supports 1,022 characters.
-Prompt
The -Prompt
parameter is used to display a message to the console. While I recommend always providing parameter names for readability, the -Prompt
parameter is positional, which means you can provide the value without typing the parameter name. Surprisingly, the prompt parameter is not required — but I can’t imagine many situations where prompting someone for input without any kind of explanation is very useful.
-AsSecureString
The -AsSecureString
parameter ensures input is saved as a SecureString versus the default string type. The -AsSecureString
parameter also hides user input with asterisks.
While the Read-Host
help documentation indicates that the -AsSecureString
parameter is secure enough to accept passwords as input, some sources disagree and discourage trusting this property as completely secure, especially in PowerShell (formally PowerShell Core).
-MaskInput
The -MaskInput
parameter masks user input by replacing it with asterisks. The -MaskInput
parameter is only available in PowerShell (not Windows PowerShell) and was added in version 7.1.
I’ll be honest — I find this parameter kind of strange. While the parameter does hide the input, it does nothing to secure the input, which you can see in this image.
I’ve tried to think of a situation where I would use this parameter, but I can’t think of any that wouldn’t make my security analyst glare at me. If the input is worth hiding, isn’t it worth securing? Let me know if you think of a good use for this parameter.
You can find me over at PDQ’s Discord server. Feel free to join and hang out with fellow sysadmins and PowerShell enthusiasts!
Now that we’ve gone over the boring stuff (I’m just kidding Jeffrey Snover, please don’t hurt me), let’s dive into some examples.
PowerShell Read-Host example: Assign input to a variable
Using Read-Host
to assign input to a variable is one of the most basic functions of Read-Host
. It’s also super beneficial. Here’s a simple example of how to assign input to a variable with Read-Host
.
$name = Read-Host -Prompt “What is your name”
As you can see, this is a very simple example. We created a variable called $name
, and assigned it the input of the Read-Host
cmdlet. We added the -Prompt
parameter with the message “What is your name.” Remember, Read-Host
automatically appends a colon to the end of a prompt. After running the command, we provided the name Dumbledore that was passed to the $name
variable. When we call the variable, it returns the name we provided as input.
This might be an easy example, but it’s the basic structure we’ll use for many of our more complex examples.
PowerShell Read-Host example: Validate input
Validating user input is critical to ensure your PowerShell scripts run correctly. For example, if you ask a user’s age and they put in their name instead, you likely won’t achieve the results you’re looking for.
Here’s an example of how to validate user input in PowerShell with Read-Host
.
$number = Read-Host "Enter a number from 1 to 99"
while ($number -notmatch "^(\d?[1-9]|[1-9]0)$"){
$number = Read-Host "Invalid entry. Enter a number from 1 to 99"
}
Write-Host "You entered $number"
This example is a bit more complex, but you’ll notice it starts with the same structure as our basic example. The main difference is that after the Read-Host
command, we jump into a while
loop for our input validation. In the while
loop, we compare the input to a regex pattern that ensures the input is a number from 1 to 99. If the input fails validation, Read-Host prompts the user that the entry was invalid and to input a new entry.
Comparison operators and regex are great ways to validate user input. Check out our PowerShell regex article to see a few more examples, including how to validate email addresses and phone numbers.
PowerShell Read-Host example: Stop specified processes
Read-Host
becomes very valuable when you act on the input provided. Here’s a sample script that asks the user to input a process name and then stops the specified process.
$process = Read-Host -Prompt "Enter a process to stop"
if (Get-Process $process -ErrorAction SilentlyContinue){
Stop-Process -Name $process
$process = $process.Substring(0,1).ToUpper() + $process.Substring(1).ToLower()
Write-Output "$process has been stopped."
} else{
$process = $process.Substring(0,1).ToUpper() + $process.Substring(1).ToLower()
Write-Output "$Process is not a running process."
}
In this script, we prompt for a process name to stop. Next, we use an If
statement to check if the process is running. If the process is running, it’s stopped. If it’s not running, we return that information to the console. We also added a command to capitalize the output of the process name because it’s the small touches that make me smile.
PowerShell Read-Host example: Check if a computer is online
This is an example of how we can use Read-Host
to identify if a target computer is online or not. Notice that we reused a lot of the code from the previous example even though this script’s purpose is quite different.
$computer = Read-Host -Prompt "Enter a computer name"
if (Test-Connection -Target $computer -Quiet){
$computer = $computer.ToUpper()
Write-Output "$computer is online."
} else{
$computer = $computer.ToUpper()
Write-Output "$computer is offline."
}
Similar to the previous example, we take the input from Read-Host
and pass it to an If
statement where we test the connection of the provided computer name with the Test-Connection
cmdlet. Then we use Write-Output
to display a message stating whether the computer is online or not.
PowerShell Read-Host example: Combining user input with switch statements
For our last example, I’ll show you how to compare user input to conditions in a switch
statement. The ability to take different actions depending on the provided input is super beneficial, but make sure to validate the input. The nice thing about a switch
statement is that we can use a default condition, which catches any invalid entries. Here’s the script.
$age = Read-Host -Prompt "Enter your age and I'll recommend an activity"
while ($age -notmatch "^(\d?[1-9]|[1-9]0)$"){
$age = Read-Host "Invalid entry. Enter your age"
}
switch ($age) {
{1..5 -contains $_}{"Kids movie"}
{6..10 -contains $_}{"Trampoline park"}
{11..20 -contains $_}{"Water park"}
{21..40 -contains $_}{"Boating"}
{41..70 -contains $_}{"Broadway show"}
{71..99 -contains $_}{"Fancy dinner"}
default {"Input not recognized. Exiting script."}
}
In this example, we reuse the age verification regex pattern to validate the input. When a valid entry is detected, it’s compared against the switch statement conditions. When a condition is met, the output is returned for the matching condition.
Combining Read-Host
input with switch
statements makes for a super powerful combination with endless possibilities.
What will you do with your newfound PowerShell superpowers?
With great PowerShell comes great responsibility. What will you do with all these new PowerShell skills? Well, if you manage devices, you could use PDQ Connect to deploy awesome scripts to your Windows devices. Sound sweet? Try Connect out for yourself, absolutely free.