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.
Help non-PowerShell users navigate your scripts more easily.
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.
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.
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:
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.”
}
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’)
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.
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:
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.
business.com is a trusted resource for small businesses. Our dedicated experts research and test SMB solutions so you can make smart, confident decisions. With business.com+, members get dedicated support, exclusive deals and expert advice. We do the work so you can focus on growing your business.