Using the Read-Host Cmdlet

Prompting a User to Enter Information

The Read-Host cmdlet enables you to interactively prompt a user for information. For example, this command prompts the user to enter his or her name, then stores that name in the variable $Name (to answer the prompt, type a name and then press ENTER):

$Name = Read-Host "Please enter your name"

Read-Host requires just one parameter: the prompt to be presented to the user. Note that you do not have to add a colon at the end of the prompt (e.g., “Please enter your name:”); Windows PowerShell will add the colon to the end of the prompt for you.

Within the Windows PowerShell console, that looks like this:

By adding the -assecurestring parameter you can mask the data entered at the prompt. For example, this command uses the -assecurestring parameter to ask a user to enter his or her password:

$Password = Read-Host -assecurestring "Please enter your password"

And here’s what that looks like on-screen: