If you are writing an interactive script, the language you use has to accept user input. In PowerShell, the most important cmdlet for this purpose is Read-Host. In some cases, mandatory function parameters and Out-GridView are helpful.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de.

Whereas the old command-line interface only offers the bare essential functionality for this task, PowerShell is able to accept and evaluate user input under various conditions.

Reading input with Read-Host ^

The Read-Host cmdlet provides the most common features for requesting user input in PowerShell. In the simplest case, you can even call it without parameters. However, the user then won’t see a prompt indicating that the script is expecting input. Thus, you will usually want to add the -Prompt parameter:

This command will send the entered text to Stdout, which is the computer screen by default. If you want to process the input, for instance by splitting or replacing strings, you first have to save it in a variable:

Prompting for passwords ^

Read-Host also allows you to store passwords as a Secure String. Instead of displaying the input in plain text, PowerShell will represent the password characters with “*”:

The object of the type System.Security.SecureString can be used for a variety of tasks, including resetting AD passwords and encrypting passwords.

Storing a password as Secure String

Storing a password as a Secure String

Using functions for user input ^

In some cases, you don’t have to explicitly call a cmdlet to request user input. For instance, if you declare a function parameter as mandatory and you call the function without a value for the parameter, PowerShell will request user input:

In the above example, the Get-Message function expects the parameter -Message and its value must not be the empty character string. However, if you call the function without arguments, PowerShell will prompt the user to enter a value for the parameter.

Requesting user input through a mandatory function parameter

Requesting user input through a mandatory function parameter

You can use the -HelpMessage parameter attribute to tell the user what kind of input your script expects. However, PowerShell doesn’t display this help text automatically. The user has to enter “!?”; otherwise, he will only see the name of the required parameter as a prompt. Thus, it is recommended to use a telling parameter name.

A simple menu with Out-GridView ^

For the special case where a user only has to choose among specified values in a graphical menu, the Out-GridView cmdlet is suitable. Its purpose usually is to display data in a table with the ability to sort the rows by clicking a column title.

If you call Out-GridView with the -passthru switch, the cmdlet returns the value that the user selected before clicking the OK button.

Using Out-GridView to display a menu

Using Out-GridView to display a menu

In this example, Out-GridView receives four string objects through the pipeline and displays them in a graphical list. It is not possible to set the window size and it might therefore look a bit oversized for small lists. The -Title parameter determines the window label.

You will usually need the user’s selection to control the further processing of your script, for which you could work with switch statements. For this purpose, you have to assign the user selection to a variable:

Win the monthly 4sysops member prize for IT pros

Share
0

Related Posts

3 Comments
  1. Tobias Hochgürtel 2 years ago

    Helped me, thank you!

    0

  2. daniel 2 years ago

    Hi Wolfgang. Thanks very much for this post. So many useless post ,  I could not this HelpMessage  - apparently no one is bothered by this, but you solved this for me.

    0

  3. Saumen 1 year ago

    This Post really helped me out, Thanks a lot.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account