PoshCode Archive  Artifact [d5690e34d1]

Artifact d5690e34d1a9bd2efab56f242d8f479a363408c83b65b57096b5bf26ccd03501:

  • File New-Choice.ps1 — part of check-in [9176ad4232] at 2018-06-10 13:13:50 on branch trunk — Creates a Choice Dialog for end users. It will return the string that the user chose. (user: Andy Schneider size: 1578)

# encoding: ascii
# api: powershell
# title: New-Choice
# description: Creates a Choice Dialog for end users. It will return the string that the user chose.
# version: 0.1
# type: function
# author: Andy Schneider
# license: CC0
# function: New-Choice
# x-poshcode-id: 2659
# x-archived: 2011-05-10T18:18:21
# x-published: 2011-05-06T10:43:00
#
#
function New-Choice {
<#
	.SYNOPSIS
		The New-Choice function is used to provide extended control to a script author who writing code 
                  that will prompt a user for information.

	.PARAMETER  Choices
		An Array of Choices, ie Yes, No and Maybe

	.PARAMETER  Caption
		Caption to present to end user

	.EXAMPLE
		PS C:\> New-Choice -Choices 'Yes','No' -Caption "PowerShell makes choices easy"
		
	.NOTES
		Author: Andy Schneider
		Date: 5/6/2011
#>

[CmdletBinding()]
param(
		
	[Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)]
	$Choices,
		
	[Parameter(Position=1)]
	$Caption,
    
	[Parameter(Position=2)]
	$Message    
	
)
	
process {
        
        $resulthash += @{}
        for ($i = 0; $i -lt $choices.count; $i++) 
            {
        	   $ChoiceDescriptions += @(New-Object System.Management.Automation.Host.ChoiceDescription $choices[$i])
               $resulthash.$i = $choices[$i]
            }
        $AllChoices = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceDescriptions)
        $result = $Host.UI.PromptForChoice($Caption,$Message, $AllChoices, 1)
        $resulthash.$result
        }         
}