Create Menu with PowerShell

Tagged: 

Viewing 1 reply thread
  • Author
    Posts
    • #1577505
      AvatarIT Consultant
      Participant
      Member Points: 529
      Rank: 2

      Hi, I’m trying to create a menu using PowerShell, and I have an issue with the list number.

      The script is like this :

      # Define the title and options for the menu
      $Title = "Select an option:"
      	$Options = @(
      	[System.Management.Automation.Host.ChoiceDescription]::new("&A", "Option A"),
      	[System.Management.Automation.Host.ChoiceDescription]::new("&B", "Option B"),
      	[System.Management.Automation.Host.ChoiceDescription]::new("&C", "Option C")
      )
      
      # Display the menu and get the user's selection
      $Selection = $Host.UI.PromptForChoice($Title, "", $Options, 0)
      
      # Handle the user's selection
      Switch ($Selection) {
      	0 { Write-Host "You selected Option A" }
      	1 { Write-Host "You selected Option B" }
      	2 { Write-Host "You selected Option C" }
      }

      Unfortunately, it shows the menu like this:

      [1] 1 [2] 2 [3] 3 [4] 4 [?] Help (default is “1”):

      I would like to have something like this:

      [1] Option 1  [2]  Option 2 [3]  Option 3 [4]  Option 4 [?] Help (default is “1”):

      Any help, please?

    • #1577552
      AvatarMichael Pietroforte
      Keymaster
      Member Points: 45,161
      Author of the year 2018
      Rank: 4

      Try this:

      # Define the title and options for the menu
      $Title = "Select an option:"
      $Options = @(
          [System.Management.Automation.Host.ChoiceDescription]::new("&1. Option A", "Description for Option A"),
          [System.Management.Automation.Host.ChoiceDescription]::new("&2. Option B", "Description for Option B"),
          [System.Management.Automation.Host.ChoiceDescription]::new("&3. Option C", "Description for Option C")
      )
      
      # Display the menu and get the user's selection
      $Selection = $Host.UI.PromptForChoice($Title, "", $Options, 0)
      
      # Handle the user's selection
      Switch ($Selection) {
          0 { Write-Host "You selected Option A" }
          1 { Write-Host "You selected Option B" }
          2 { Write-Host "You selected Option C" }
      }
      

      In this modification, the labels of the options include numbers (“&1. Option A”, &2. Option B, etc.). This way, the menu should display the options as [1] Option A, [2] Option B, and [3] Option C.

      The & symbol in the label indicates the hotkey for that option, which should correspond to the number you want to display. The description field can be used to provide additional information about each option, but it’s not typically displayed in the menu.

      avatar
      • #1577590
        AvatarIT Consultant
        Participant
        Member Points: 529
        Rank: 2

        Thank you for your help and assistance.

        a quick question, I would like that based on user choice, the script should do something, how I can tackle this?

        • #1577592
          AvatarMichael Pietroforte
          Keymaster
          Member Points: 45,161
          Author of the year 2018
          Rank: 4

          Try this:

          # Define the title and options for the menu
          $Title = "Select an option:"
          $Options = @(
              [System.Management.Automation.Host.ChoiceDescription]::new("&1. Option A", "Description for Option A"),
              [System.Management.Automation.Host.ChoiceDescription]::new("&2. Option B", "Description for Option B"),
              [System.Management.Automation.Host.ChoiceDescription]::new("&3. Option C", "Description for Option C")
          )
          
          # Display the menu and get the user's selection
          $Selection = $Host.UI.PromptForChoice($Title, "", $Options, 0)
          
          # Handle the user's selection
          Switch ($Selection) {
              0 { 
                  Write-Host "You selected Option A"
                  # Add your code for Option A here
                  # Example:
                  # Write-Host "Performing action for Option A"
              }
              1 { 
                  Write-Host "You selected Option B"
                  # Add your code for Option B here
                  # Example:
                  # Write-Host "Performing action for Option B"
              }
              2 { 
                  Write-Host "You selected Option C"
                  # Add your code for Option C here
                  # Example:
                  # Write-Host "Performing action for Option C"
              }
          }
          
        • #1577612
          AvatarIT Consultant
          Participant
          Member Points: 529
          Rank: 2

          Thank you very much

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account