business.com receives compensation from some of the companies listed on this page. Advertising Disclosure

How to Build an Interactive Menu Inside a PowerShell Script

Updated Jul 06, 2023

Table of Contents

Open row

PowerShell modules and scripts aren’t generally written with interactivity in mind. After all, automation is generally about hands-off activities. However, there are times when you might need to write a script for people less familiar with PowerShell. You could write a GUI for your script, but that can get complicated. 

Fortunately, Microsoft PowerShell provides a way to build small, interactive menus inside a PowerShell script, allowing users to provide input quickly and easily. Here’s how it works.

Bottom LineBottom line

Adding interactive menus to your PowerShell scripts is a great way to make them accessible to your team without investing in employee training.

How to build an interactive menu in a PowerShell script

There are a few ways to create an interactive menu in PowerShell. For example, you can use a .NET object System.Management.Automation.Host.ChoiceDescription and the PromptForChoice method. While this method works, designing your own interactive menu gives you more control over your messaging. 

We’ll show you how to design a custom interactive menu using a single function called Show-Menu and a do/until PowerShell loop. You’ll include a few choices and an option to quit the menu anytime. The goal is to present users with several options and let them run those options, and then present the menu again so they can run other options.

1. Build a PowerShell function. 

To get started, you need a way to build this menu in a PowerShell function. You must build the menu in a function because whenever the user selects an option – and the script runs that option – we must show the menu again.

Here is a very simple Show-Menu function. You’ll notice that, essentially, it does two things: 

  • Clears the host screen 
  • Writes text to the screen

It doesn’t need to do anything more. If you’d like, you could make this better by moving up the options as a parameter, making it more reusable, but that’s up to you.

function Show-Menu
{
     param (
           [string]$Title = ‘My Menu’
     )
     cls
     Write-Host “================ $Title ================”
    
     Write-Host “1: Press ‘1’ for this option.”
     Write-Host “2: Press ‘2’ for this option.”
     Write-Host “3: Press ‘3’ for this option.”
     Write-Host “Q: Press ‘Q’ to quit.”
}

TipBottom line

To make your scripts more customizable, make the Show-Menu function reusable by moving up the menu options as parameters.

2. Create a loop.

Once the PowerShell function is built, you need a way to display the menu, provide user input, execute the task and display the menu again. A do/until loop can handle all these elements. 

A do/until loop executes code immediately. In this case, it shows the menu and then uses Read-Host to prompt for a selection.

do
{
     Show-Menu
     $input = Read-Host “Please make a selection”
     switch ($input)
     {
           ‘1’ {
                cls
                ‘You chose option #1’
           } ‘2’ {
                cls
                ‘You chose option #2’
           } ‘3’ {
                cls
                ‘You chose option #3’
           } ‘q’ {
                return
           }
     }
     pause
}
until ($input -eq ‘q’)

FYIDid you know

The do/until loop is important because it keeps the menu on the screen until the user decides to close it.

3. Finalize your menu. 

You’re using a switch statement to make a decision on what code to execute based on the option selected. This is where you might run another script, reboot a server, set a registry key, delete a file, etc.

Also, always be sure to include a “Q” option. This is a way for the user to break out of the loop if they’re done with the menu. And notice the pause keyword; this is to prevent the menu from displaying extremely quickly again when it isn’t necessarily required.

Finally, the do loop – similar to loops you might use in programming when syncing folders in PowerShell – will continuously loop until the “Q” option is selected. Once the “Q” option is selected, the “until” construct will be satisfied, ending the loop.

The benefits of interactive menus inside PowerShell scripts

Not everyone is comfortable with programming or PowerShell. However, creating interactive menus for co-workers can help them take advantage of the scripts you create.

In addition to reducing complexity, interactive menus make it easy for users to:

  • Navigate a system without requiring you to build a full GUI interface
  • Choose from the range of options available more quickly
  • Get more work done, because a series of choices minimizes the need for code input

For devs, interactive menus also simplify adding or deleting links to particular scripts and functions as you upload them to or remove them from the system.

Mark Fairlie contributed to this article. 

Adam Bertram
Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.
BDC Logo

Get Weekly 5-Minute Business Advice

B. newsletter is your digest of bite-sized news, thought & brand leadership, and entertainment. All in one email.

Back to top