PoshCode Logo PowerShell Code Repository

New-Choice by Andy Schneider 6 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2660"></script>download | new post

New-Choice Update

  1.  
  2. function New-Choice {
  3. <#
  4.         .SYNOPSIS
  5.                 The New-Choice function is used to provide extended control to a script author who writing code
  6.         that will prompt a user for information.
  7.  
  8.         .PARAMETER  Choices
  9.                 An Array of Choices, ie Yes, No and Maybe
  10.  
  11.         .PARAMETER  Caption
  12.                 Caption to present to end user
  13.  
  14.         .EXAMPLE
  15.                 PS C:\> New-Choice -Choices 'Yes','No' -Caption "PowerShell makes choices easy"
  16.                
  17.         .NOTES
  18.                 Author: Andy Schneider
  19.                 Date: 5/6/2011
  20. #>
  21.  
  22. [CmdletBinding()]
  23. param(
  24.                
  25.         [Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)]
  26.         $Choices,
  27.                
  28.         [Parameter(Position=1)]
  29.         $Caption,
  30.    
  31.         [Parameter(Position=2)]
  32.         $Message    
  33.        
  34. )
  35.        
  36. process {
  37.        
  38.         $resulthash += @{}
  39.         for ($i = 0; $i -lt $choices.count; $i++)
  40.             {
  41.                    
  42.                $ChoiceDescriptions += @(New-Object System.Management.Automation.Host.ChoiceDescription ("&" + $choices[$i]))
  43.                $resulthash.$i = $choices[$i]
  44.             }
  45.         $AllChoices = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceDescriptions)
  46.         $result = $Host.UI.PromptForChoice($Caption,$Message, $AllChoices, 1)
  47.         $resulthash.$result -replace "&", ""
  48.         }        
  49. }
  50.  
  51. new-choice -Choices "yes","no","maybe"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me