PowerTip: Use PowerShell to Obtain User Input

PowerTip: Use PowerShell to Obtain User Input

  • Comments 12
  • Likes

Summary: Learn how to use Windows PowerShell to solicit user input.


Hey, Scripting Guy! QuestionHow can I solicit input from the user?

Hey, Scripting Guy! Answer Use the Read-Host cmdlet:

$in = Read-host “enter the data”


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed

    another variant:

    PS II> Add-Type -AssemblyName Microsoft.VisualBasic

    PS II> $mvi = [Microsoft.VisualBasic.Interaction]::InputBox("enter user name", "User", "$env:username")

    PS II> $mvi

    PS II> $v=$host.ui.ReadLine()

    PS II> $v

  • [system.console]::Writeline('What is you name?')




  • @jrv

    thank's for sharing:

    but ReadKey(),Read() and Writeline() not return input

  • power shell will be very much  required.............

  • @Wallid - works just fine:

    function GetInput($msg){

      [system.console]::Writeline('What is you name?')



      Write-Host "You typed:'$inp'" -fore green


  • hi


    yes, it work, but the answer must be a simple expression not function :)


    here another variant:

    PS II>  saps $env:ComSpec -no -wai -arg '/v /c "set/p _h=$msg:&echo(!_h!"'

  • Get user input:


    Works jsut fine and it is a simple line.  

    When is a function not an expression?  It is just a special case of an expression.  Anywhere where PowerShell or any other language says this syntax tag <expression>  we can substitute a function.  If yuo are thikning the aan expresion needs an "=" sign then you misunderstand the definition of expression.

    the numerb 1 is an experssion in its simplewst form.  Statign and object at a PowerShell causes evealuation and what is typed must be a legitimate expression.  These are all legitimate expressions.





  • @jrv

    "  It is just a special case of an expression"

    --->  yes,a function is a Swiss army knife, and in our case, a simple expression can be enough :)

    why create a function when you can do it with simple expressions

    sorry for my english, if you find errors is the error google translate :)

  • @Wallid

    At first I did not use a function and you said it didin't return anything.  I wrapped it is a function to deonstrate to you that is does work.

    This was my first post.


    Try it - it works.

  • @jrv


    ----> yes, this work but this

    $t=[system.console]::Writeline('What is you name?')






    --> don't work

    thank for sharing jrv

  • @Wallid - why would you expect WriteLine to return a string or anything.  Does Write-Host return a value?

    15:19 PS>[system.console]::Read()



    15:19 PS>

    Read returns a single byte from the buffer.


    Returns a single decoded character. ($true suppresses echo)

    We use them like this:

    [system.console]::WriteLine('Press any key to continue');[system.console]::ReadKey()|Out-Null

    Here is a nice way to elimiminate casing issues:

    [system.console]::WriteLine('Press any key to continue');while(($d=[system.console]::ReadKey($true)).Key -ne 'X'){$d.KeyChar}

    Both x and X will terminate the read while preserving the case of the character.

    So there are many, many ways to get user input.

    Like: ---->>>> www.designedsystemsonline.com/.../ConsolePlay.txt

  • hi @jrv

    "So there are many, many ways to get user input."

    ----> you're right


    ---> readkey return a "ConsoleKeyInfo" type not a string like read-host


    ---> is like write-host and is not a user input like "readline" or "read-host".

    you can combine the two "WriteLine" and "Readline"

    [system.console]::WriteLine('Press any key to continue');

    while(($d=[system.console]::ReadKey($true)).Key -ne 'Enter'){write-host $d.KeyChar -no}

    but is criptic