POWERSHELL   43
New Menu
Guest on 3rd February 2023 01:45:41 PM


  1. using namespace System.Management.Automation.Host
  2.  
  3. function New-Menu {
  4.     [CmdletBinding()]
  5.     param(
  6.         [Parameter(Mandatory)]
  7.         [ValidateNotNullOrEmpty()]
  8.         [string]$Title,
  9.  
  10.         [Parameter(Mandatory)]
  11.         [ValidateNotNullOrEmpty()]
  12.         [string]$Question
  13.     )
  14.    
  15.     $red = [ChoiceDescription]::new('&Red', 'Favorite color: Red')
  16.     $blue = [ChoiceDescription]::new('&Blue', 'Favorite color: Blue')
  17.     $yellow = [ChoiceDescription]::new('&Yellow', 'Favorite color: Yellow')
  18.  
  19.     $options = [ChoiceDescription[]]($red, $blue, $yellow)
  20.  
  21.     $result = $host.ui.PromptForChoice($Title, $Question, $options, 0)
  22.  
  23.     switch ($result) {
  24.         0 { 'Your favorite color is Red' }
  25.         1 { 'Your favorite color is Blue' }
  26.         2 { 'Your favorite color is Yellow' }
  27.     }
  28.  
  29. }

Raw Paste

Login or Register to edit or fork this paste. It's free.