Read-Host

Read a line of input from the console.

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, -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. SecureStrings can therefore be passed around with impunity.

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

"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



Back to the Top

© Copyright SS64.com 1999-2010
Some rights reserved