PowerShell Prompts – Part 2

In Part 1 of this series, I talked about an Approve-Action function.  This is great for a single go/no-go approval.  Unfortunately not all decisions are so boolean.  For more advanced problems, I developed the Confirm-UserInput function, which instead focuses on validation input versus action approval.  The purpose of this function is to enforce user input to conform to a list of available options, with the objective being less code overall for the script author.  Take for example the following scenario:

I want to know which color you want to make your background.  Let’s say my script respects only red, green and blue.  You could have some code looking similar to the following:

$userColor = read-host “Enter the color (red, green or blue)”

if ($userColor –eq “red”) {


if ($userColor –eq “green”) {


Another option would be:

if ($userColor –match “red|green|blue”) {

$Background.Color = $userColor


else {

#error or pick default color


In either scenario, if the user provides bad input you can either default to some color or you would be forced to set up a loop to prompt the user again.  When I wrote this function, my goal was to remove a bit of the headache for the script author on which code path could possibly taken depending on who is running the console.

Here is the function, and example for our background color scenario:

function Confirm-UserInput {
        $Message = "Enter one of the following choices.",
        [string[]]$Choices = @("&Yes","&No","&Cancel")
    $ListChoices = @()
    foreach ($Choice in $Choices) {
        $ListChoices += [System.Management.Automation.Host.ChoiceDescription]$Choice
    return $Choices[$Host.UI.PromptForChoice(`

$Background.Color = (Confirm-UserInput –Message “Choose a valid color.” –Choices “&Blue”,”&Green”,”&Red”)

Once this command executes, I know I can immediately assign the return to the Color property, as the return has to be one of the choices I defined.


Comments (1)

Skip to main content