Read-Host
Published: February 29, 2012
Updated: August 15, 2012
Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0
Read-Host
Syntax
Parameter Set: Default Read-Host [[-Prompt] <Object> ] [-AsSecureString] [ <CommonParameters>]
Detailed Description
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.
Parameters
-AsSecureString
Displays asterisks (*) in place of the characters that the user types as input.
When you use this parameter, the output of the Read-Host cmdlet is a SecureString object (System.Security.SecureString).
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Prompt<Object>
Specifies the text of the prompt. Type a string. If the string includes spaces, enclose it in quotation marks. Windows PowerShell appends a colon (:) to the text that you enter.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
1 |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Inputs
The input type is the type of the objects that you can pipe to the cmdlet.
-
None
You cannot pipe input to this cmdlet.
Outputs
The output type is the type of the objects that the cmdlet emits.
-
System.String or System.Security.SecureString
If the AsSecureString parameter is used, Read-Host returns a SecureString. Otherwise, it returns a string.
Examples
-------------------------- EXAMPLE 1 --------------------------
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.
PS C:\> $age = read-host "Please enter your age"
-------------------------- EXAMPLE 2 --------------------------
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.
PS C:\> $pwd_secure_string = read-host "Enter a Password" -assecurestring
Related topics