Today I wanted to improve my choice prompts in some of the scripts I write. There are a few ways to achieve this such as using a Read-Host to gather input and simply using an if statement, but that doesn’t cover any form of error checking without expanding the code further. So I went hunting and settled on using $host.ui.PromptForChoice instead, as this will handle error checking for me only allowing the choices I offer. It also handles a default option and best of all, it’s not case sensitive.
So lets dive in and have a look at the PowerShell function I’ve written to handle a yes no choice.
Lets see this function in action. First of all we will run this without the -DefaultOption parameter set. In this case the function will default 0 (No).
Function Read-YesNoChoice {
<#
.SYNOPSIS
Prompt the user for a Yes No choice.
.DESCRIPTION
Prompt the user for a Yes No choice and returns 0 for no and 1 for yes.
.PARAMETER Title
Title for the prompt
.PARAMETER Message
Message for the prompt
.PARAMETER DefaultOption
Specifies the default option if nothing is selected
.INPUTS
None. You cannot pipe objects to Read-YesNoChoice.
.OUTPUTS
Int. Read-YesNoChoice returns an Int, 0 for no and 1 for yes.
.EXAMPLE
PS> $choice = Read-YesNoChoice -Title "Please Choose" -Message "Yes or No?"
Please Choose
Yes or No?
[N] No [Y] Yes [?] Help (default is "N"): y
PS> $choice
1
.EXAMPLE
PS> $choice = Read-YesNoChoice -Title "Please Choose" -Message "Yes or No?" -DefaultOption 1
Please Choose
Yes or No?
[N] No [Y] Yes [?] Help (default is "Y"):
PS> $choice
1
.LINK
Online version: https://www.chriscolden.net/2024/03/01/yes-no-choice-function-in-powershell/
#>
Param (
[Parameter(Mandatory=$true)][String]$Title,
[Parameter(Mandatory=$true)][String]$Message,
[Parameter(Mandatory=$false)][Int]$DefaultOption = 0
)
$No = New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'No'
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', 'Yes'
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($No, $Yes)
return $host.ui.PromptForChoice($Title, $Message, $Options, $DefaultOption)
}
PS C:\Users\Chris.Colden> $choice = Read-YesNoChoice -Title "Please Choose" -Message "Yes or No?"
Please Choose
Yes or No?
[N] No [Y] Yes [?] Help (default is "N"): y
PS C:\Users\Chris.Colden> $choice
1
Now lets try setting the -DefaultOption parameter to 1 (Yes).
PS C:\Users\Chris.Colden> $choice = Read-YesNoChoice -Title "Please Choose" -Message "Yes or No?" -DefaultOption 1
Please Choose
Yes or No?
[N] No [Y] Yes [?] Help (default is "Y"):
PS C:\Users\Chris.Colden> $choice
1
This isn’t very useful on it’s own and we will want to do something with that choice. You can use if statements if you like, but I prefer to run the users choice through a switch.
switch($choice)
{
# No
0 {
# Your code when No is chosen
}
# Yes
1 {
# Your code when Yes is chosen
}
}
Pretty simple hey? Thats about it for now. Let me know in the comments how you are using this function in your own code.
Happy PowerShelling and more importantly Happy Choosing!