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 navigate your scripts more easily.

author image
Written by: Adam Bertram, Senior WriterUpdated Jan 02, 2025
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 graphical user interface (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. 

“Building an interactive menu in a PowerShell script can be a fun and engaging way to enhance user experience,” said William Mabotja, an Azure-certified senior software developer at Atlas Finance. “Here are the key steps: Define the Menu Structure, Create a Display Function, Implement a Loop, Capture User Input and Execute Commands.”

Jason Wingate, CEO of product development specialists Emerald Ocean, stressed the importance of getting the foundations right and offered three core components to ensure this. “First, map out your menu structure — what options users need and how they flow,” Wingate explained. “Then implement the input handling using Read-Host or Out-GridView (I prefer Read-Host for its simplicity). Finally, wrap it in a loop so users can keep making selections until they’re done.”

Now, 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.

“Interactive menus are lightweight and quick to develop — you’re not dealing with the overhead of building out a full GUI,” Wingate noted. “They’re especially useful for rapidly deploying solutions and are ideal for resource-constrained environments. For example, they’re perfect for older terminals in a warehouse system — they get the job done without the complexity of a full GUI.”

Mabotja advised keeping the menu concise and intuitive. “When designing interactive menus, developers should follow several best practices,” he offered. “Ensure each input option has a default value, keep menus concise and provide clear labeling. Implement robust error handling to gracefully manage invalid input.”

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. You could improve this 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 decide 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.

“I would focus on clarity and user experience,” Wingate advised. “Don’t overcomplicate everything. Keep options clear and concise. Always validate user input — remember, users will find ways to break things if you don’t!” 

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.

“Interactive menus really help with user adoption,” said Wingate. “They make scripts accessible to those who might be intimidated by command-line interfaces. You’re essentially creating a bridge between powerful PowerShell functionality and users who may not be PowerShell experts.”

If you’re new to PowerShell or want to explore more advanced uses, check out our comprehensive introduction to PowerShell

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