Public Group
Active 3 days, 11 hours ago
PowerShell group
Create Menu with PowerShell
Tagged: powershell
- This topic has 4 replies, 2 voices, and was last updated 6 days, 18 hours ago by
IT Consultant.
- AuthorPosts
- Sun, Nov 19 2023 at 11:28 am #1577505
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?
- Tue, Nov 21 2023 at 9:46 pm #1577552
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.
- Thu, Nov 23 2023 at 10:20 am #1577590
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?
- Fri, Nov 24 2023 at 1:07 am #1577592
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" } }
- Fri, Nov 24 2023 at 11:15 pm #1577612
Thank you very much
- AuthorPosts
- You must be logged in to reply to this topic.