I came across this little function today whilst doing some house cleaning.   It’s a function that will show a message prompt and uses a switch statement to control what actions are taken.  It’s an unsupported Microsoft script but no idea where it came from.

Function GetChoice {
 #Prompt message
 $Caption = “Restart the computer.”
 $Message = “It will take effect after restart,  do you want to restart right now?”
 $Choices = [System.Management.Automation.Host.ChoiceDescription[]] @(“&Yes”,“&No”)
 [Int]$DefaultChoice = 0
 $ChoiceRTN = $Host.UI.PromptForChoice($Caption$Message, $Choices, $DefaultChoice)
 Switch ($ChoiceRTN){
  0  { shutdown -t 0 -r }
  1 {break}

Anyway, clearly this is ripe for customisation.  Step one lets make the function name conform to the PowerShell convention by adding a hyphen
Function Get-Choice

Now lets look at what else might be useful …..

Well the caption and message and even the choices could be made into parameters so you can change the ‘choices’

param (
 $Caption = “Restart the computer.”,
$Message = “It will take effect after restart, do you want to restart right now?”,
$Choices = @(“&Yes”,“&No”,“&Maybe”)
change the line that instantiated the $choices variable to

$Choices = [System.Management.Automation.Host.ChoiceDescription[]] $Choices

Hopefully you can see the flaw in the logic now though – the switch statement can’t be customised, well not easily I could pass in a script block but there’s little point in that.

But you could either directly return the choice value as an integer

$Host.UI.PromptForChoice($Caption, $Message, $Choices, $DefaultChoice)

Or return the value

$Choices[$Host.UI.PromptForChoice($Caption, $Message, $Choices, $DefaultChoice)].Label