This post aims to describe the possible way to prompt user input in PowerShell.
How to Prompt for User Input in PowerShell?
PowerShell supports a wide range of cmdlets to automate various tasks. The Read-Host cmdlet is used to prompt user input in PowerShell. This section briefly describes the working and usage of the Read-Host cmdlet to prompt user input in PowerShell.
How to use the Read-Host cmdlet in PowerShell?
As discussed above, the Read-Host cmdlet is the key contender in getting the user input using PowerShell.
Example 1: How to prompt a User for input using Read-Host cmdlet?
In this example, you can ask the user to enter his favorite topic. Here is the example code.
You can see in the above snippet that the user inputs his favorite topic and in return, the same input is printed on the console as an output.
PowerShell stores the prompt command in a variable that can also be used in the future, the example is as follow
In the above code, the prompt statement is stored in a variable named $topic. As the user input is stored in the $topic, now call the $topic anytime you need.
Example 2: How to obtain password in a secure manner?
This example demonstrates how to take input of sensitive information of a user. To do so, the following code is used:
$code = Read-Host "Please enter your security code " -AsSecureString
In this code, the user input is stored in a variable name $userName. The user also asked to put its security code which will be stored in the $code variable. The parameter -AsSecureString is used to convert the security code into a secure string.
As you can see the user entered his name as input and also his security code but that cannot be seen. In PowerShell, –AsSecureString can be very helpful to secure your string data or confidential data in this regard.
Example 3: How to prompt for user input in Switch case
The following example will explain the user input in accordance with the switch conditional statement.
switch ($colors)
{
"green"
{ "Green is nature, healing, freshness" }
"orange"
{ "Orange is confidence, success, bravery" }
"yellow"
{ "Yellow is creativity, happiness, warmth" }
"white"
{ "White is clean, simplicity, honest" }
"black"
{ "Black is formality, sophistication, security" }
default
{ "You did not choose your favorite color" }
}
The $color variable stores the user input about the favorite color. The user input compares the best match with the help of switch statements and returns when it is found.
The output screenshot illustrates that when a user asked about their favorite color. The switch statement compares the user input (yellow) value in the given conditions and the best option matches printed out as output.
By the end, you have learned how to prompt a user for input by using Read-Host in PowerShell.
Conclusion
In PowerShell users can be asked to input values. With Read-Host, one can prompt a user for input. This post demonstrates various usages of the Read-Host cmdlet to prompt user input in PowerShell. The Read-Host cmdlet offers a variety of options that are also described in this guide. Importantly, the Read-Host cmdlet offers you to securely input the password by hiding the keywords.