Take the 2-minute tour ×

I want to prompt the user for a series of inputs, including a password and a filename.

I have an example of using host.ui.prompt, which seems sensible, but I can't understand the return.

Is there a better way to get user input in PowerShell?

share|improve this question
give a look here: poshcode.org/608 –  CB. Nov 18 '11 at 15:08
add comment

4 Answers

up vote 77 down vote accepted

Read-Host is a simple option for getting string input from a user.

$name = Read-Host 'What is your username?'

To hide passwords you can use:

$pass = Read-Host 'What is your password?' -AsSecureString

To convert the password to plain text:


Edit: As for the type returned by $host.UI.Prompt(), if you run the code at the link posted in @Christian's comment, you can find out the return type by piping it to Get-Member (ex. $results|gm). The result is a Dictionary where the key is the name of a FieldDescription object used in the prompt. To access the result for the first prompt in the linked example you would type: $results['String Field']

Edit 2: To access information without invoking a method leave the parentheses off.

PS> $Host.UI.Prompt

MemberType          : Method
OverloadDefinitions : {System.Collections.Generic.Dictionary[string,psobject] Pr
                    ompt(string caption, string message, System.Collections.Ob
                    ldDescription] descriptions)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : System.Collections.Generic.Dictionary[string,psobject] Pro
                    mpt(string caption, string message, System.Collections.Obj
                    dDescription] descriptions)
Name                : Prompt
IsInstance          : True

$Host.UI.Prompt.OverloadDefinitions will give you the definition(s) of the method. Each definition displays as <Return Type> <Method Name>(<Parameters>)

share|improve this answer
Thanks, @Rynant. Accepted answer for being the only one who actually answered my main question! ;) All the other information is really helpful too, especially as I'm still groping my way in PS. –  AJ. Nov 23 '11 at 12:38
No problem, @AJ. Another way to get information about a method is to leave off the parentheses. I'll add an example to my answer. –  Rynant Nov 23 '11 at 14:09
add comment

Using parameter binding is definately the way to go here. Not only is it very quick to write (Just add [Parameter(Mandatory=$true)] in above your mandatory parameters), but it's also the only option that you won't hate yourself for later.

More below:

[Console]::ReadLine is explicitly forbidden by the FxCop rules for PowerShell. Why? Because it only works in PowerShell.exe, not PowerShell ISE, PowerGUI, etc.

Read-Host is, quite simply, bad form. Read-Host oncontrollably stops the script and prompts the user, which means that you can never have another script that includes the script that uses Read-Host.

What you're trying to do is ask for parameters.

You should use the [Parameter(Mandatory=$true)] attribute, and correct typing, to ask for the parameters.

If you use this on a [SecureString], it will prompt for a password field. If you use this on a Credential type ([Management.Automation.PSCredential]), the credentials dialog will be popped up if the parameter isn't there. A string will just become a plain old text box. If you add a HelpMessage to the parameter attribute (i.e. [Parameter(Mandatory=$true,HelpMessage='New User Credentials')]) then it will become some helper text for the prompt.

Hope this Helps

share|improve this answer
Definitely useful. Many thanks. I suspected as much, but appreciate having it spelled out. –  AJ. Nov 23 '11 at 12:38
This is the most flexible and user-friendly solution, but I almost ignored your advice because there were no clear code examples like in Rynant's answer. Can you provide some nicely formatted examples? –  Iain Elder Oct 22 '13 at 15:31
add comment

Place this at the top of your script. It will cause the script to prompt the user for a password. The resulting password can then be used elsewhere in your script via $pw.

     [Parameter(Mandatory=$true, Position=0, HelpMessage="Password?")]

   $pw = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))

   write-host $pw

Don't forget to remove the last line, write-host $pw. It's there solely for debugging.

share|improve this answer
add comment

You can use:

$userinput = [Console]::ReadLine()


$userinput = Read-Host "Enter input here:"
share|improve this answer
add comment

Your Answer


