Use this function to get Yes/No confirmation from the user.
Use parameter -untilValidOption to force user to select one valid option
# Snippet ::: Get User Confirmation YesNo function -_- # PowerShellExamples.com function Get-UserConfirmationYesNo { <# .SYNOPSIS Get User confirmation asking for Y(es)/N(o) .DESCRIPTION Use this function to get Yes/No confirmation from the user. .NOTES Use parameter -untilValidOption to force user to select one valid option .LINK https://powershellexamples.com/home/Article/21/snippet-get-user-confirmation-YesNo-Function .EXAMPLE Call function to Get User Confirmation Asking once with Default Message $proceed = Get-UserConfirmationYesNo # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } .EXAMPLE Call function to Get User Confirmation Asking Until Valid Option is selected with Custom Message $proceed = Get-UserConfirmationYesNo -untilValidOption -message "Do you want to continue?" # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } #> param( [switch]$untilValidOption, [string]$message = "Are you Sure You Want To Proceed?:" ) do { Write-Host $message -ForegroundColor Yellow $confirmation = Read-Host "Y(es)/N(o):" switch -regex ($confirmation.ToLower()) { "^y(es)?$" { $proceed = $true } "^n(o)?$" { $proceed = $false } default { if($untilValidOption.IsPresent){ write-host "Option not valid. Try again" -ForegroundColor red } $proceed = $null } } } while ($proceed -eq $null -and $untilValidOption.IsPresent) return $proceed } # Call function to Get User Confirmation Asking Once -_- # with Default message -_- $proceed = Get-UserConfirmationYesNo # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } # Call function to Get User Confirmation Asking Until Valid Option is selected -_- # with Custom Message -_- $proceed = Get-UserConfirmationYesNo -untilValidOption -message "Do you want to continue?" # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } }
Copy code below into a power shell session and execute
# Sample : Snippet ::: Get User Confirmation YesNo function -_- # PowerShellExamples.com # Get User Confirmation Yes/No - Function function Get-UserConfirmationYesNo { <# .SYNOPSIS Get User confirmation asking for Y(es)/N(o) .DESCRIPTION Use this function to get Yes/No confirmation from the user. .NOTES Use parameter -untilValidOption to force user to select one valid option .LINK https://powershellexamples.com/home/Article/21/snippet-get-user-confirmation-YesNo-Function .EXAMPLE Call function to Get User Confirmation Asking once with Default Message $proceed = Get-UserConfirmationYesNo # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } .EXAMPLE Call function to Get User Confirmation Asking Until Valid Option is selected with Custom Message $proceed = Get-UserConfirmationYesNo -untilValidOption -message "Do you want to continue?" # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } #> param( [switch]$untilValidOption, [string]$message = "Are you Sure You Want To Proceed?:" ) do { Write-Host $message -ForegroundColor Yellow $confirmation = Read-Host "Y(es)/N(o):" switch -regex ($confirmation.ToLower()) { "^y(es)?$" { $proceed = $true } "^n(o)?$" { $proceed = $false } default { if($untilValidOption.IsPresent){ write-host "Option not valid. Try again" -ForegroundColor red } $proceed = $null } } } while ($proceed -eq $null -and $untilValidOption.IsPresent) return $proceed } # Call function to Get User Confirmation Asking Once -_- # with Default message -_- Write-Host "`r`n`r`nCall function to Get User Confirmation Asking Once -_-" $proceed = Get-UserConfirmationYesNo # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } } # Call function to Get User Confirmation Asking Until Valid Option is selected -_- # with Custom Message -_- Write-Host "`r`n`r`nCall function to Get User Confirmation Asking Until Valid Option is selected -_-" $proceed = Get-UserConfirmationYesNo -untilValidOption -message "Do you want to continue?" # take action from response -_- if([string]::IsNullOrEmpty($proceed)){ Write-Host "Selected Option is not valid" }else{ if($proceed){ Write-Host "Proceed." }else{ Write-Host "Cancel Operation. Not Proceed." } }
PS C:\>
Call function to Get User Confirmation Asking Once -_-
Are you Sure You Want To Proceed?:
Y(es)/N(o):: Yes
Proceed.
Call function to Get User Confirmation Asking Until Valid Option is selected -_-
Do you want to continue?
Y(es)/N(o):: ys
Option not valid. Try again
Do you want to continue?
Y(es)/N(o):: No
Cancel Operation. Not Proceed.