Reads a line of input from the console.
Read-Host [[-Prompt] [<Object>]] [-AsSecureString] [<CommonParameters>]
The Read-Host cmdlet reads a line of input from the console. You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords, as well as shared data.
You cannot pipe input to this cmdlet.
System.String or System.Security.SecureString
If the AsSecureString parameter is used, Read-Host returns a SecureString. Otherwise, it returns a string.
- Save console input to a variable:
PS C:> $Age = Read-Host "Please enter your age"
This command displays the string “Please enter your age:” as a prompt. When a value is entered and the Enter key is pressed, the value is stored in the $Age variable.
- Save console input as a secure string:
PS C:> $pwd_secure_string = Read-Host "Enter a Password" -AsSecureString
This command displays the string “Enter a Password:” as a prompt. As a value is being entered, asterisks (*) appear on the console in place of the input. When the Enter key is pressed, the value is stored as a SecureString object in the $pwd_secure_string variable.