Converting the Windows Script Host ReadLine Method

Definition: Returns an entire line from an input stream.

ReadLine

The ReadLine method is typically used to read data typed in by the user. In that regard, ReadLine is analogous to PowerShell’s Read-Host cmdlet. In the following command Read-Host prompts the user to enter his or her name, waits until the user has typed in something and pressed ENTER, and then stores that value in a variable named $a:

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

Note. The variable $a will contain only the characters that were typed in; it will not include the newline character sent when the user pressed ENTER. This is also the way the ReadLine method works.

Incidentally, Read-Host does include at least one feature not found in ReadLine: by adding the –asSecureString parameter Read-Host will mask any data typed in by the user:

$a = Read-Host "Please enter your name" -asSecureString

When you run the preceding command your screen should something like this, with the asterisk serving as the character mask:

Please enter your name: ********

Of course, $a will not be equal to ********; instead, it will be equal to the actual value typed in by the user.

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page