PowerTip: Use PowerShell to Obtain User Input


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”

 

Comments (12)

  1. jrv says:

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

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

    s

    115

    15:19 PS>

    Read returns a single byte from the buffer.

    ([system.console]::ReadKey($true)).KeyChar

    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: —->>>> http://www.designedsystemsonline.com/…/ConsolePlay.txt

  2. jrv says:

    @Wallid – works just fine:

    function GetInput($msg){

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

      [system.console]::Clear()

      $inp=[system.console]::ReadLine()

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

    }

  3. jrv says:

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

    [system.console]::ReadLine()

    [system.console]::Read()

    [system.console]::ReadKey()

  4. jrv says:

    @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.

    [system.console]::ReadLine()

    Try it – it works.

  5. kishhr says:

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

  6. jrv says:

    Get user input:

    [system.console]::ReadLine()

    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.

    1

    A

    $x=1

    Get-Process

  7. Walid Toumi says:

    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

  8. Walid Toumi says:

    @jrv

    thank's for sharing:

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

  9. Walid Toumi says:

    hi

    @JRV

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

    @eveyone

    here another variant:

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

  10. Walid Toumi says:

    @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 🙂

  11. Walid Toumi says:

    @jrv

    "[system.console]::ReadLine()"

    —-> yes, this work but this

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

    $t

    $t=[system.console]::Read()

    $t

    $t=[system.console]::ReadKey()

    $t

    –> don't work

    thank for sharing jrv

  12. Walid Toumi says:

    hi @jrv

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

    —-> you're right

    "[system.console]::ReadKey"

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

    "[system.console]::WriteLine"

    —> 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