Skip to content

Read-Host

SYNOPSIS

Reads a line of input from the console.

SYNTAX

AsString (Default)

Read-Host [[-Prompt] <Object>] [-MaskInput] [<CommonParameters>]

AsSecureString

Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]

DESCRIPTION

The `Read-Host` cmdlet reads a line of input from the console (stdin). 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.

> [!NOTE] > `Read-Host` has a limit of 1022 characters it can accept as input from a user.

EXAMPLES

Example 1: Save console input to a variable

$Age = Read-Host "Please enter your age"

Example 2: Save console input as a secure string

$pwd_secure_string = Read-Host "Enter a Password" -AsSecureString

Example 3: Mask input and as a plaintext string

$pwd_string = Read-Host "Enter a Password" -MaskInput

PARAMETERS

-AsSecureString

Indicates that the cmdlet 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 ).

Type: System.Management.Automation.SwitchParameter
Parameter Sets: AsSecureString
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-MaskInput

Indicates that the cmdlet 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 String object. This allows you to safely prompt for a password that is returned as plaintext instead of SecureString .

Type: System.Management.Automation.SwitchParameter
Parameter Sets: AsString
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-Prompt

Specifies the text of the prompt. Type a string. If the string includes spaces, enclose it in quotation marks. PowerShell appends a colon (`:`) to the text that you enter.

Type: System.Object
Parameter Sets: (All)
Aliases:

Required: False
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

None

This cmdlet does not accept input from the PowerShell pipeline.

OUTPUTS

System.String or System.Security.SecureString

If the AsSecureString parameter is used, `Read-Host` returns a SecureString . Otherwise, it returns a string.

NOTES

This cmdlet only reads from the stdin stream of the host process. Usually, the stdin stream is connected to the keyboard of the host console.

Clear-Host

Get-Host

Write-Host

ConvertFrom-SecureString

Back to top