If you want a quick ready-made solution, you can copy paste mine:
<#
.SYNOPSIS
Asks a question and waits for user's answer
.EXAMPLE
Usage with shortcuts and without ReturnValue
Invoke-Question -Question "What would you like" -Answers "&Eggs", "&Toasts", "&Steak"
Shows the quesiton and waits for input. Let's assume user input is 'S', the return value would be 2 (index of "&Steak")
.EXAMPLE
Usage without shortcuts and with ReturnValue
Invoke-Question -Question "What would you like" -Answers "Eggs", "Toasts", "Steak" -ReturnValue
Shows the quesiton and waits for input. The answers are prefixed with numbers 1, 2 and 3 as shortcuts.
Let's assume user input is 2, the return value would be "Toasts" (prefixed numbers are "index + 1")
.EXAMPLE
Usage from pipeline with default value
@("Eggs", "Toasts", "Steak") | Invoke-Question -Question "What would you like" -ReturnValue -Default 2
Shows the quesiton and waits for input. The answers are taken from pipeline and prefixed with numbers 1, 2 and 3 as shortcuts.
Steak is marked as default. If user simply continues without a choice, Steak is chosen for her.
However, let's assume user input is 1, the return value would be "Eggs" (prefixed numbers are "index + 1")
#>
function Invoke-Question {
[CmdletBinding()]
param(
# Main question text
[Parameter(Mandatory = $true)]
[string] $Question,
# Question description, e.g. explanation or more information
[Parameter(Mandatory = $false)]
[string] $Description = "",
# Default answer as index in the array, no answer is selected by default (value -1)
[Parameter(Mandatory = $false)]
[int] $Default = -1,
# Set of answers, if the label is given with & sign, the prefixed letter is used as shortcut, e.g. "&Yes" -> Y,
# otherwise the answer is prefixed with "index + 1" number as a shortcut
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]] $Answers,
# If set, returns a value of selected answer, otherwise returns its index in the Answer array
[switch] $ReturnValue
)
begin {
# init choices
$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$answerNumber = 1
$rememberAnswers = @()
}
process {
#init answers
foreach ($answer in $answers) {
$rememberAnswers += $answer
if ($answer -notmatch "&") {
# add number if shortcut not specified
$answer = "&$answerNumber $answer"
}
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList $answer))
$answerNumber++
}
}
end {
# ask question and return either value or index
$index = $Host.UI.PromptForChoice($Question, $Description, $choices, $Default)
if ($ReturnValue) {
$rememberAnswers[$index]
} else {
$index
}
}
}