Powershell

Read-Host: A Great Way to Get Input to Your PowerShell Scripts

The “Read-Host” cmdlet is a built-in command in PowerShell used to prompt the user to enter the input. This cmdlet performs two operations at a time. First, it pauses the script execution and second it prompts the user to enter input. It reads a single line from the console. The major role of this command is to collect information. Moreover, it also assists in collecting sensitive information, such as passwords.

This blog will discuss several methods to get input from the user using the “Read-Host” cmdlet.

How to Get Input to PowerShell Scripts Using “Read-Host” Cmdlet?

The “Read-Host” command is utilized in PowerShell to take input from the user whether it is a simple text or confidential information such as a password.

Example 1: Prompt User to Enter Name

This example will give a demonstration to prompt a user to enter a name using the “Read-Host” cmdlet:

Read-Host "Please input your name"

Add the “Read-Host” cmdlet to prompt a user with the specified message added within the inverted command. This message will display while taking input from the user:

Example 2: Prompt User for Input

Now, we will prompt a user to enter name and age and then change the color of output:

Write-Host "This program will prompt for the name."

$name= Read-Host "Enter your name"

$age= Read-Host "Enter your age"

Write-Host "User name is" $name -ForegroundColor Yellow

Write-Host "User age is" $age -ForegroundColor Yellow

According to the above code:

  • First, add the “Write-Host” cmdlet and specify the string that needs to be displayed.
  • After that, add two variables “$name” and “$age” and assign the “Read-Host” command to them to prompt the user to enter their name and age.
  • Then, utilize the “Write-Host” command to add a text string and use the “-ForegroundColor” to change the text color in the output:

Example 3: Prompt User to Enter Password

Now, add the following command in the script:

$pwd = Read-Host "Input Password" -AsSecureString

According to the above code:

  • First, add the variable “$pwd”, assign the “Read-Host” command, add a string that will display while taking the input from the user and add “-AsSecureString” parameter.
  • More specifically, the “-AsSecureString” parameter takes the input as a password and converts it to a secure string:

Enter the password and hit the “OK” button.

Let’s check whether the password was created or not by executing the variable “$pwd”:

$pwd

That was all about getting input to PowerShell scripts.

Conclusion

To get input from the user, the “Read-Host” cmdlet is used for simple text input or a password. To do so, add the “Read-Host” cmdlet. After that, specify the text you want to display at the time of taking input from the user. Lastly, use the “-AsSecureString” parameter to convert the input password to a secure string. This blog has delivered a great way to take input to the PowerShell scripts.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.