BDC Hamburger Icon

Menu

Close
BDC Logo
Search Icon
Advertising Disclosure
Close
Advertising Disclosure

Business.com aims to help business owners make informed decisions to support and grow their companies. We research and recommend products and services suitable for various business types, investing thousands of hours each year in this process.

As a business, we need to generate revenue to sustain our content. We have financial relationships with some companies we cover, earning commissions when readers purchase from our partners or share information about their needs. These relationships do not dictate our advice and recommendations. Our editorial team independently evaluates and recommends products and services based on their research and expertise. Learn more about our process and partners here.

How to Build an Interactive Menu Inside a PowerShell Script

Help non-PowerShell users more easily navigate your scripts.

author image
Written by: Adam Bertram, Senior WriterUpdated Jul 06, 2023
Gretchen Grunburg,Senior Editor
Business.com earns commissions from some listed providers. Editorial Guidelines.
Table Of Contents Icon

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. 

Did you find this content helpful?
Verified CheckThank you for your feedback!
author image
Written by: Adam Bertram, Senior Writer
Adam Bertram is an IT expert and business owner who has spent decades advising on network administration and security, designing and building infrastructure, and creating and teaching courses on Windows Server, Powershell and more. While maintaining his own IT business, he has provided hands-on DevsOps services for clients like JPMorgan Chase. At business.com, Adam covers the ins and outs of PowerShell, helping companies improve their Windows configurations and automations. Bertram, who has a degree in computer science, holds Microsoft, Cisco and CompTIA credentials. He has written numerous tutorials, guides and books, including "Building Better PowerShell Code: Applying Proven Practices One Tip at a Time."
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