Skip to content

Instantly share code, notes, and snippets.

@Kqpa
Created March 8, 2022 19:15
Show Gist options
  • Save Kqpa/ca72bfe7eb35eab064e4bf5e7fbe52a2 to your computer and use it in GitHub Desktop.
Save Kqpa/ca72bfe7eb35eab064e4bf5e7fbe52a2 to your computer and use it in GitHub Desktop.
Yes/no function in PowerShell
function Read-FromUserChoice {
.SYNOPSIS
param (
[Parameter(Mandatory)]$Message,
$Caption,
[string[]]$Options = @('&Yes','&No'),
[string]$DefaultOption = '&Yes'
)
$option = $Options.IndexOf($DefaultOption)
if ($option -eq '-1') {
throw 'The default option specified is not in the list of supplied options'
}
$Choice = $host.UI.PromptForChoice($Message, $Caption, $Options, $option)
return $options[$Choice] -replace '&',''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment