Read a line of input from the console. Prompt the user for input.
Syntax
Read-Host [[-prompt] Object] [-asSecureString] [CommonParameters]
Key
-prompt Object
The string that will become the prompt object. If the prompt
string includes spaces, it must be surrounded by quotes.
-asSecureString
If set to true, the input will be echoed as star characters (*).
The output will then be a Securestring object.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
Password data should never be stored as a regular string, as it would be visible in memory.
A SecureString is a type of string that PowerShell
(and .Net) keep encrypted in memory.
Examples
Present the string "Please enter your name:" as a prompt. When a value is entered and the Enter key is pressed, that value is stored in the $myname variable:
PS C:\> $myname = read-host "Please enter your name:"
Presents the string "Enter a Password:" as a prompt. When a password is entered and the Enter key is pressed, the value is stored as a SecureString:
PS C:\> $my_password = read-host "Enter a Password:" -assecurestring
A function to prompt for a user name (using '[string]' guarantees the function will return a string even if '123' is entered):
function getUsername {
Param ([string]$name=(Read-Host "Enter a user name"))
write-host "You entered $name"
}
“There is no off position on the genius switch” - David Letterman
Related:
Clear-Host - Clear the screen
ConvertFrom-SecureString - Convert a secure string into an encrypted standard string
Get-Host - Get host information
Out-Host - Send the pipelined output to the host
Write-Host - Display objects through the host user interface
Equivalent bash command: read - Read a line from standard input