PowerShell PromptForChoice PowerShell PromptForChoice powershell powershell

PowerShell PromptForChoice


Assuming you have less than 10 choices to pick from, you can prepend &N and generate the choice descriptions on the fly with hot keys then numbered 1 - 9:

$choiceIndex = 1$options = $all_subscriptions |ForEach-Object {    New-Object System.Management.Automation.Host.ChoiceDescription "&$($choiceIndex++) - $($_.Name)"}$chosenIndex = $host.ui.PromptForChoice($title, $message, $options, 0)$SubscriptionToUse = $all_subscriptions[$chosenIndex]


Looks like you can just pass a string array as choices.

[string[]]$choices = "machine", "another machine", "the third one", "obviously this one!"$result = $host.ui.PromptForChoice($title, $message, $choices, 0)Write-Host $choices[$result] -ForegroundColor Green

$result is an integer representing the indices of the choice. We can then refer back to our array and grab the item at that position and hey-presto - you get your result!