- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a User Menu in PowerShell?
When you are writing a script and if you want to provide the user to select one option among multiple values and based on it to execute the command, we can generally use the Switch command and for it, we will ask the user choice in the below script.
There is another .Net method to create a user menu, we will see it after the example from the description mentioned above.
a. Switch command method
For the Switch command-based user selection, we will first display the message for the user and then give him a choice for the selection through Read-Host command as shown below.
Write-Host "============= Pick the Server environment==============" Write-Host "`ta. 'P' for the Prod servers" Write-Host "`tb. 'T' for the Test servers" Write-Host "`tc. 'D for the Dev Servers'" Write-Host "`td. 'Q to Quit'" Write-Host "========================================================" $choice = Read-Host "`nEnter Choice"
Once we have the blueprint ready, we will use the Switch command to execute steps based on the user selection.
switch ($choice) { 'P'{ Write-Host "`nYou have selected a Prod Environment" } 'T'{ Write-Host "`nYou have selected a Test Environment" } 'D'{ Write-Host "`nYou have selected a Dev Environment" } 'Q'{Return} }
If you want users restriction to select among the given values then you can use the do-until loop. You can also set the condition using different cmdlets and as per your criteria. We will write below the entire script and with user choice restriction.
do { Write-Host "`n============= Pick the Server environment==============" Write-Host "`ta. 'P' for the Prod servers" Write-Host "`tb. 'T' for the Test servers" Write-Host "`tc. 'D for the Dev Servers'" Write-Host "`td. 'Q to Quit'" Write-Host "========================================================" $choice = Read-Host "`nEnter Choice" } until (($choice -eq 'P') -or ($choice -eq 'T') -or ($choice -eq 'D') -or ($choice -eq 'Q') ) switch ($choice) { 'P'{ Write-Host "`nYou have selected a Prod Environment" } 'T'{ Write-Host "`nYou have selected a Test Environment" } 'D'{ Write-Host "`nYou have selected a Dev Environment" } 'Q'{ Return } }
Output
============= Pick the Server environment============== a. 'P' for the Prod servers b. 'T' for the Test servers c. 'D for the Dev Servers' d. 'Q to Quit' ======================================================== Enter Choice: r ============= Pick the Server environment============== a. 'P' for the Prod servers b. 'T' for the Test servers c. 'D for the Dev Servers' d. 'Q to Quit' ======================================================== Enter Choice: T You have selected a Test Environment
b. .NET menu option
With the .Net namespace, we can also automate the same above selection menu and this is the actual standard process. For that, we will leverage the System.Management.Automation.Host namespace of the .Net framework and then we need to use class [ChoiceDescription] of it. Its syntax is as below.
[ChoiceDescription]::new('Label', 'HelpMessage')
You can learn more about the above class from the URL given below.
using namespace System.Management.Automation.Host $Prod = [ChoiceDescription]::new('&Prod', 'Environment:Prod') $Test = [ChoiceDescription]::new('&Test', 'Environment:Test') $Dev = [ChoiceDescription]::new('&Dev', 'Environment:Dev') $Envs = [ChoiceDescription[]]($prod,$Test,$Dev) $choice = $host.ui.PromptForChoice("Select Environment", "Prod?, Dev?, Test?", $envs, 0)
In the above example, we are creating different variables for the environment and merging them to the array called $envs, and later to show UI prompt we need to use PromptForChoice class or function.
Syntax for PromptForChoice class.
PSHostUserInterface.PromptForChoice(String, String, Collection<ChoiceDescription>, Int32)
Below URL for more information about the PromptForChoice class.
So, the entire script will be like,
using namespace System.Management.Automation.Host $Prod = [ChoiceDescription]::new('&Prod', 'Environment:Prod') $Test = [ChoiceDescription]::new('&Test', 'Environment:Test') $Dev = [ChoiceDescription]::new('&Dev', 'Environment:Dev') $Envs = [ChoiceDescription[]]($prod,$Test,$Dev) $choice = $host.ui.PromptForChoice("Select Environment", "Prod?, Dev?, Test?", $envs, 0) switch ($choice) { 0{ Write-Host "`nYou have selected a Prod Environment" } 1{ Write-Host "`nYou have selected a Test Environment" } 2{ Write-Host "`nYou have selected a Dev Environment" } }
Output
You have selected a Dev Environment
If you have noticed the difference between the two switch commands in both the methods mentioned above, in the first case we have used Read-Host parameter to read the choice and then passed it to the Switch command and based on the choice it selects the Switch command condition but in the second example, we have used the numbering, It is related to the choice that user enters. If the user selected the first choice, here “Prod” then it goes to the value ‘0’ and for the second value it’s ‘1’ and for the third value, it is ‘2’ and so on.
- Related Articles
- How to create a new local user in windows using PowerShell?
- Create a command/menu item that the user can invoke from a popup menu in HTML5
- How to create a Popup Menu in Tkinter?
- How to create a Menu using JavaFX?
- How to create a Vertical menu bar in Java?
- How to create a submenu for a Menu in Java
- How to create a Menu Icon with CSS?
- How to create a vertical menu with CSS?
- How to create a "fixed" menu with CSS?
- How to create a subnavigation menu with CSS?
- How to create a dropup menu with CSS?
- How to create a Dictionary in PowerShell?
- How to create a user in MongoDB v3?
- How to create Menu Item Component in ReactJS?
- How to create a horizontal scrollable menu with CSS?
