My Technical Notes

Monday, 8 May 2017

PowerShell Confirm Step Without using `ShouldProcess`, `ShouldContinue` etc


$title = "Do you want to go to Disney?";

# to not show a prompt, set $prompt to an empty string.
$prompt = 'Please select one of the following options...'

$choiceType = [System.Management.Automation.Host.ChoiceDescription];
$doOption = New-Object $choiceType '&Yes','You want to go to Disney';
$cancelOption = New-Object $choiceType '&No','You do NOT want to go to Disney';
$unsureOption = New-Object $choiceType 'I am &Unsure','You are unsure about going to Disney';

$options = @($doOption,$cancelOption, $unsureOption)

$defaultOption = 0;
$choiceInt = $Host.UI.PromptForChoice($title,$prompt,$options,$defaultOption)
$choice = $options[$choiceInt];

if ($choice -eq $doOption) {
    Write-Host "fly to Disney";
} elseif ($choice -eq $cancelOption) {
    Write-Host "Unpack bags";
} elseif ($choice -eq $unsureOption) {
    Write-Host "Ask me later";
}

No comments: