More Choices in PowerShell


A while back I wrote a few articles on creating a menu driven script in the PowerShell console. In those articles, I used Read-Host to prompt the user for a choice.

If you don’t mind digging into the .NET Framework, then that’s not your only option. You have most likely encountered something like this in PowerShell:

Typical PowerShell prompt for choice
Typical PowerShell prompt for choice (Image Credit: Jeff Hicks)

You enter one of the bracketed values and something happens. With a little scripting on your part, you can use this same concept in your scripts. The method is part of the builtin $host variable.

The PromptForChoice method
The PromptForChoice method (Image Credit: Jeff Hicks)

The PromptForChoice() method has several variations. The parameters we’ll use are essentially a caption, a message prompt, a collection of choices, and the default choice. Let’s play.

The most difficult part of the process is creating the collection of choices. This will be a collection of [System.Management.Automation.Host.ChoiceDescription] objects. First, I’ll initialize an array.

Next, I’ll create one of these ChoiceDescription objects and save the result to a variable.

The text “Choice &1” will be displayed in the prompt. The & indicates what character to use for the choice. Whatever immediately follows will be used as you’ll see. In this case, the number 1 will be the option. What does $c look like?

The choice object
The choice object (Image Credit: Jeff Hicks)

The HelpMessage is empty, so I’ll set a value.

The choice object with a help message
The choice object with a help message (Image Credit: Jeff Hicks)

The last step is to add the choice to the collection.

It doesn’t make sense to have a prompt for choice menu with only a single item, so I’ll add some more.

The collection of choices
The collection of choices (Image Credit: Jeff Hicks)

Now I can prompt for a choice.

This is what I get when running in the PowerShell ISE:

ISE choice prompt
ISE choice prompt (Image Credit: Jeff Hicks)

And this is what it looks like in the PowerShell console:

PowerShell console choice prompt
PowerShell console choice prompt (Image Credit: Jeff Hicks)

Note that the ? shows the help messages. Remember when I used the & symbol to indicate what to use for a response? You’ll see those characters in the brackets. In my code, the answer is stored in $r. The value will be an integer reflecting the index number of the selected array member. You can use a simple Switch statement to process the results.

Remember that arrays start counting at 0. Here’s the complete script in action.

The choice prompt in action
The choice prompt in action (Image Credit: Jeff Hicks)

I am merely displaying a message but you could invoke whatever PowerShell commands you needed to.

But we can take advantage of PowerShell’s ability to extend objects and add some action to our object choices. Here’s a more practical example.

I’ll reset the collection variable.

I’ll create my first entry and add a help message.

Before I add it to the array, I’m going to add a new member to the object using Add-Member.

I’ve added a new method called Invoke that will run the scriptblock assigned to the Value parameter. I can even test it.

Testing the new method
Testing the new method (Image Credit: Jeff Hicks)

The reason for adding the action will become clearer in a moment. For now, I’ll add some additional choices.

I can prompt for a choice:

Because the value of $r is the index number of the selected item from $coll, I can run my Invoke method.

Invoking the selected choice
Invoking the selected choice (Image Credit: Jeff Hicks)

Here’s what it looks like from the console.

Demo in the console
Demo in the console (Image Credit: Jeff Hicks)

Not too bad. But if I want the ability to bring the menu back I’ll need a loop.

The prompt will keep looping until the last item is selected, which is my Quit choice.

Looping (Image Credit: Jeff Hicks)

To wrap this up, let’s combine the console based menu idea I demonstrated in the past with these techniques.

A sample menu script
A sample menu script (Image Credit: Jeff Hicks)

So if you are looking to build some tools for the Help Desk or even yourself, I hope these examples will prove useful. As always, please let me know what you think in the comments.

Related Topics:

  • PowerShell

    Don't have a login but want to join the conversation? Sign up for a Petri Account