Back to Menu
Connecting You To Opportunity
What can we help you find?
| Login|Sign Up
Back to Menu
Hello
  • Login
  • Sign Up

How To Build An Interactive Menu Inside Of A PowerShell Script

By
Adam Bertram
,
business.com writer
|
Sep 05, 2018
Home
> Technology
SHARE THIS

PowerShell scripts aren't generally written with interactivity in mind.

Learn how to design and build your own interactive menus inside of your PowerShell scripts to help non-PowerShell users more easily navigate and use them.

PowerShell scripts aren't generally written with interactivity in mind. Automation is all about hands-off activities, but there are times when you might need to write a script not for you but for others who might not know PowerShell like you do. You could write a GUI for your script but that can get complicated pretty quickly. Maybe you just need a way for a user to quickly provide some input to a script. To do this, you can build small interactive menus inside of a PowerShell script -- here's how.

How to build an interactive menu

There's a few ways to create an interactive menu in PowerShell. One way is to use a .NET object System.Management.Automation.Host.ChoiceDescription and the PromptForChoice method. This is way is fine but I believe you have a little more control over the messaging if you design your own. This is what we'll be covering today.

The menu that we'll be creating will be built using a single function called Show-Menu and a do/until loop. We'll include a few options to choose from and an option to quit from the menu at any time. The premise is to present the user with a few options, allow them to run those option and then present the menu again if they'd like to run other options.

To get started, you need a way to build this menu in a PowerShell function. This needs to be in a function because whenever the user selects an option and the script runs whatever is in that option, we need to show the menu again.

Here is a very simple Show-Menu function. You'll notice that essentially it just does two things: Clears the host screen and then just writes some text to the screen. It needs to do nothing more. You could make this better by moving the options up as a parameter if you'd like, which would make 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 function is built, we'll then need a way to display it, provide user input and then, when whatever is going to happen actually happens when an option is chosen, we want to display the menu again. This is why we chose to use a do/until loop.

A do/until loop executes code right off the bat. 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')

We're then 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, but when it isn't necessarily required.

Final step

Finally, the do loop will continually loop over and over again until the "Q" option. Once the "Q" option is selected, the until construct will be satisfied, which will then break out of the loop.

Adam Bertram
Adam Bertram
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.
Like the article? Sign up for more great content.Join our communityAlready a member? Sign in.
We'd love to hear your voice!
Login to comment.
LoginSign Up