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
  • <p>Hi Ed</p> <p>another variant:</p> <p>PS II&gt; Add-Type -AssemblyName Microsoft.VisualBasic</p> <p>PS II&gt; $mvi = [Microsoft.VisualBasic.Interaction]::InputBox(&quot;enter user name&quot;, &quot;User&quot;, &quot;$env:username&quot;)</p> <p>PS II&gt; $mvi</p> <p>PS II&gt; $v=$host.ui.ReadLine()</p> <p>PS II&gt; $v</p>

  • <p>[system.console]::Writeline(&#39;What is you name?&#39;)</p> <p>[system.console]::ReadLine()</p> <p>[system.console]::Read()</p> <p>[system.console]::ReadKey()</p>

  • <p>@jrv</p> <p>thank&#39;s for sharing:</p> <p>but ReadKey(),Read() and Writeline() not return input</p>

  • <p>power shell will be very much &nbsp;required.............</p>

  • <p>@Wallid - works just fine:</p> <p>function GetInput($msg){</p> <p> &nbsp; [system.console]::Writeline(&#39;What is you name?&#39;)</p> <p> &nbsp; [system.console]::Clear()</p> <p> &nbsp; $inp=[system.console]::ReadLine()</p> <p> &nbsp; Write-Host &quot;You typed:&#39;$inp&#39;&quot; -fore green</p> <p>}</p>

  • <p>hi</p> <p>@JRV</p> <p>yes, it work, but the answer must be a simple expression not function :)</p> <p>@eveyone</p> <p>here another variant:</p> <p>PS II&gt; &nbsp;saps $env:ComSpec -no -wai -arg &#39;/v /c &quot;set/p _h=$msg:&amp;echo(!_h!&quot;&#39;</p>

  • <p>Get user input:</p> <p>[system.console]::ReadLine()</p> <p>Works jsut fine and it is a simple line. &nbsp;</p> <p>When is a function not an expression? &nbsp;It is just a special case of an expression. &nbsp;Anywhere where PowerShell or any other language says this syntax tag &lt;expression&gt; &nbsp;we can substitute a function. &nbsp;If yuo are thikning the aan expresion needs an &quot;=&quot; sign then you misunderstand the definition of expression.</p> <p>the numerb 1 is an experssion in its simplewst form. &nbsp;Statign and object at a PowerShell causes evealuation and what is typed must be a legitimate expression. &nbsp;These are all legitimate expressions.</p> <p>1</p> <p>A</p> <p>$x=1</p> <p>Get-Process</p>

  • <p>@jrv</p> <p>&quot; &nbsp;It is just a special case of an expression&quot;</p> <p>---&gt; &nbsp;yes,a function is a Swiss army knife, and in our case, a simple expression can be enough :)</p> <p>why create a function when you can do it with simple expressions</p> <p>sorry for my english, if you find errors is the error google translate :)</p>

  • <p>@Wallid</p> <p>At first I did not use a function and you said it didin&#39;t return anything. &nbsp;I wrapped it is a function to deonstrate to you that is does work.</p> <p>This was my first post.</p> <p>[system.console]::ReadLine()</p> <p>Try it - it works.</p>

  • <p>@jrv</p> <p>&quot;[system.console]::ReadLine()&quot;</p> <p>----&gt; yes, this work but this</p> <p>$t=[system.console]::Writeline(&#39;What is you name?&#39;)</p> <p>$t</p> <p>$t=[system.console]::Read()</p> <p>$t</p> <p>$t=[system.console]::ReadKey()</p> <p>$t</p> <p>--&gt; don&#39;t work</p> <p>thank for sharing jrv</p>

  • <p>@Wallid - why would you expect WriteLine to return a string or anything. &nbsp;Does Write-Host return a value?</p> <p>15:19 PS&gt;[system.console]::Read()</p> <p>s</p> <p>115</p> <p>15:19 PS&gt;</p> <p>Read returns a single byte from the buffer.</p> <p>([system.console]::ReadKey($true)).KeyChar</p> <p>Returns a single decoded character. ($true suppresses echo)</p> <p>We use them like this:</p> <p>[system.console]::WriteLine(&#39;Press any key to continue&#39;);[system.console]::ReadKey()|Out-Null</p> <p>Here is a nice way to elimiminate casing issues:</p> <p>[system.console]::WriteLine(&#39;Press any key to continue&#39;);while(($d=[system.console]::ReadKey($true)).Key -ne &#39;X&#39;){$d.KeyChar}</p> <p>Both x and X will terminate the read while preserving the case of the character.</p> <p>So there are many, many ways to get user input.</p> <p>Like: ----&gt;&gt;&gt;&gt; <a rel="nofollow" target="_new" href="http://www.designedsystemsonline.com/upload/ConsolePlay.txt">www.designedsystemsonline.com/.../ConsolePlay.txt</a></p>

  • <p>hi @jrv</p> <p>&quot;So there are many, many ways to get user input.&quot;</p> <p>----&gt; you&#39;re right</p> <p>&quot;[system.console]::ReadKey&quot;</p> <p>---&gt; readkey return a &quot;ConsoleKeyInfo&quot; type not a string like read-host </p> <p>&quot;[system.console]::WriteLine&quot;</p> <p>---&gt; is like write-host and is not a user input like &quot;readline&quot; or &quot;read-host&quot;.</p> <p> you can combine the two &quot;WriteLine&quot; and &quot;Readline&quot; </p> <p>[system.console]::WriteLine(&#39;Press any key to continue&#39;);</p> <p>while(($d=[system.console]::ReadKey($true)).Key -ne &#39;Enter&#39;){write-host $d.KeyChar -no}</p> <p>but is criptic</p>