Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Created January 30, 2020 22:51
Show Gist options
  • Save mavaddat/fe1b4aaea1fde2a835c46b3343b2b4f6 to your computer and use it in GitHub Desktop.
Save mavaddat/fe1b4aaea1fde2a835c46b3343b2b4f6 to your computer and use it in GitHub Desktop.
Ask user for confirmation. Function allows you to ask for confirmation. It is written by @hugopeeters
# Description: Ask a user to answer a question with either yes or no.
# Example use:
# If (Ask-YesOrNo)
# {
# # User has confirmed
# ...
# }
# Else
# {
# # User did not confirm
# ...
# }
Function Ask-YesOrNo
{
param([string]$title="Confirm",[string]$message="Are you sure?")
$choiceYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Answer Yes."
$choiceNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Answer No."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($choiceYes, $choiceNo)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result)
{
0
{
Return $true
}
1
{
Return $false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment