Prompt for choice

Just a tip #5

Sometimes in the interactive session when we are executing the scripts, and where it requires an input/consent from the user to proceed further we can prompt the user to choose from the given choices…

# PromptForChoice Args
$Title = "Do you want to proceed further?"
$Prompt = "Enter your choice"
$Choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Yes", "&No", "&Cancel")
$Default = 1

# Prompt for the choice
$Choice = $host.UI.PromptForChoice($Title, $Prompt, $Choices, $Default)

# Action based on the choice
switch($Choice)
{
    0 { Write-Host "Yes - Write your code"}
    1 { Write-Host "No - Write your code"}
    2 { Write-Host "Cancel - Write your code"}
}

Share it on     |   |   |   | 
  Prev:  

Select-Object With Calculated Properties In PowerShell.

  :Next  

How To Use Splatting To Pass Parameters To Commands

comments powered by Disqus