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.

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.host.choicedescription?view=powershellsdk-7.0.0

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.

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.host.pshostuserinterface.promptforchoice?view=powershellsdk-7.0.0

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.

Updated on: 03-Jul-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements