Powershell user input options

Posted: Suzuki Date: 10.07.2017

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.

Create A User Menu In A PowerShell Script

There's a few ways to create an interactive menu in PowerShell. One way is to use a. 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. 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.

Assign roles to user accounts with Office PowerShell

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. In this case, it shows the menu and then uses Read-Host to prompt for a selection. 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.

Powershell Scripting: Creating a Menu with Powershell

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.

powershell user input options

And notice the pause keyword; this is to prevent the menu from displaying extremely quickly again, but when it isn't necessarily required. 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.

Tom's IT Pro, Real-world Business Technology. Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. By Adam Bertram September 25, Don't miss each week's most important technology trends, insights and decision-making advice, right in your inbox. See all articles in PowerShell.

Chapter 7 - Fix User laqenyberegi.web.fc2.com4

Hot Topics Cloud Computing Management Microsoft Security Servers Storage Virtualization Features Firefox Focus Browser Blocks Ads Automating Software Installs wit Dell EMC Proven Professional Cer An Introduction to AppVeyor the About Tom's IT Pro.

Rating 4,5 stars - 389 reviews
inserted by FC2 system