Interactive applications are powerful, as they keep the users engaged with your program. These applications can also provide custom results to users. Active participation with your customers in real-time can also boost their trust in your company, and more importantly, you can forge long-term relationships with them. Given these many benefits, you’d want to deploy interactive applications where possible. Most programming and scripting languages, including the popular PowerShell’s Read-Host cmdlet, make it easy to prompt users for inputs and display information accordingly.
In this article, I’ll show you how to prompt users for inputs in PowerShell using the Read-Host cmdlet.
What Is the Read-Host Cmdlet?
The Read-Host cmdlet is PowerShell’s built-in command that prompts for user inputs and collects the answers. As you can see, this is a powerful cmdlet as it has a dual use, prompting and gathering information. More importantly, the Read-Host cmdlet stores the gathered values as a secure string, which means you can also use it for collecting sensitive information such as passwords.
However, note that the Read-Host cmdlet can take a maximum of 1022 characters as input from users. This is good enough for simple prompts like passwords, names, and addresses, but isn’t as useful for large responses.
Let’s now look at the syntax and the different parameters you can use in this cmdlet.
Read-Host Cmdlet Syntax
You can use the follownig cmdlet syntax
Read-Host [[-Prompt] <Object>] [-MaskInput] [<CommonParameters>]
Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]
Both syntaxes work for the Read-Host cmdlet. The key difference is the second parameter. Both “MaskInput” and “AsSecureString” mask the input, so you’ll only see a bunch of asterisk characters instead of the actual string. However, in the first syntax, the user input gets stored as a string while in the second one, it gets stored as a SecureString object.
Equally important, the “Prompt” parameter is your prompt text: it’s the question you want your users to answer.
Now that you have an idea of the cmdlet’s parameters, let’s look at a few examples.
Examples of Prompting for User Input
You can do a ton of things with the Read-Host cmdlet. Specifically, you can ask many questions and process the answers in many ways:
Save the Inputs to a Variable
To start, the simplest use case is to store the inputs in a variable. Refer to the following for the specific code for the prompt “Please enter your name”.
The user can input the value and that value will get stored in the “Name” variable.
Prompt for a Password
When you prompt for a password, make sure that you see a bunch of asterisks after the user finishes inputting their information. You should also use the “AsSecureString” parameter if you want to store the password as a SecureString object.
The next images shows you the code for prompting to enter a password and storing the answer as a SecureString.
Use Multiple Values as Inputs
Lastly, you can use the Read-Host cmdlet to get multiple inputs from users. Check the commands in the following image.
You can also pipe these inputs into another program for further processing. You can do so much with the cmdlets, and PowerShell scripting in general, depending on what you want to gather from users and how you want to process that information.
I hope this provides a glimpse into the true capabilities of PowerShell.
To conclude, interactive applications offer many benefits for your organization. At the same time, creating such applications doesn’t have to be arduous, especially in PowerShell. In this article, I talked about the Read-Host cmdlet and how you can use it to collect information from your users. Based on this information, you can provide the necessary details or next steps. You can even store this information safely or use it for further processing. Finally, I showed you a few examples above, and I hope they helped.
Do you have any questions about prompting for user inputs in PowerShell? Check out the FAQ and Resources sections below!
What are cmdlets in PowerShell?
A cmdlet is a pre-built script that executes a specific function in PowerShell. This is a lightweight command the PowerShell runtime environment invokes. It handles many of the common functions we want, such as prompting users for inputs, changing directories, etc. These cmdlets help you save time and effort in writing scripts for frequently-used actions.
Are functions and cmdlets the same?
No, functions and cmdlets are two different things in PowerShell, though they may appear to be similar. The key difference is that you use compiled .NET to write cmdlets and PowerShell’s scripting language to write functions.
Can the Read-Host cmdlet accept inputs from the PowerShell pipeline?
No, the Read-Host cmdlet can’t accept inputs from the PowerShell pipeline. It only accepts values from users. However, you can store these in a variable or even pipe them to other functions or scripts for additional processing.
Will the Read-Host cmdlet take values through multiple input devices?
No, the Read-Host cmdlet is connected to the host’s .stdin process. That means it can take inputs only through the keyboard of the host console. It can’t take inputs from other input devices.
Can I use the “AsSecureString” and “MaskInput” parameters interchangeably?
The “AsSecureString” parameter stores the values input by the user in the form of a SecureString object. The “MaskInput” parameters store the input values as a regular String object. This difference only has a bearing when you collect and store sensitive information, like passwords. In all other cases, you can use them interchangeably.
Subscribe to our newsletters for more quality content.
TechGenix: Guide on Complex PowerShell Scripts
Find out how you can trace through a complex PowerShell script here.
TechGenix: Article on Validating PowerShell Input to Avert Problems
Learn how to validate inputs in PowerShell.
TechGenix: Guide on Using PowerShell to Create and Enable a User Account
Discover how you can create and enable a user account in PowerShell.
TechGenix: Guide on Custom PowerShell Cmdlets and Get-Help Support
Read more about adding Get-Help support to your custom PowerShell cmdlets here.
TechGenix: Article on PowerShell Versions
Check your PowerShell version so you can use the appropriate cmdlets.