# Adding a Yes, No, Cancel prompt to a PowerShell script

The estimated reading time for this post is 0 minutes

When I write PowerShell scripts I tend to want to step through sections, this allows me to decided whether to proceed with sections or not. I achieve this by using the ChoiceDescription class as demonstrated below.

[code lang=”powershell”]
## The following four lines only need to be declared once in your script.
\$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
\$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
\$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
\$options = [System.Management.Automation.Host.ChoiceDescription[]](\$yes, \$no, \$cancel)

## Use the following each time your want to prompt the use
\$title = "Title" \$message = "Question?"
\$result = \$host.ui.PromptForChoice(\$title, \$message, \$options, 1)
switch (\$result) {
0{
Write-Host "Yes"
}1{
Write-Host "No"
}2{
Write-Host "Cancel"
}
}
[/code]

More information about using prompts in PowerShell can be found in the following article and tip on Microsoft TechNet.