Interactive powershell menu
View the Project on GitHub bibistroc/powershell-interactive-menu
Install-Module -Name InteractiveMenu
This module only works in Powershell version 5.0 and up.
Sample code can be found here
# Import the module
using module InteractiveMenu
# Define the items for the menu
$answerItems = @(
[InteractiveMenuChooseMenuItem]::new("I choose good", "good", "Good info"),
[InteractiveMenuChooseMenuItem]::new("I choose bad", "bad", "Bad info")
);
# Define the question of the menu
$question = "Choos between good and bad"
# Instantiate new menu object
$menu = [InteractiveMenuChooseMenu]::new($question, $answerItems);
# [Optional] You can change the colors and the separator
$options = @{
MenuInfoColor = [ConsoleColor]::DarkYellow
QuestionColor = [ConsoleColor]::Magenta
HelpColor = [ConsoleColor]::Cyan
ErrorColor = [ConsoleColor]::DarkRed
HighlightColor = [ConsoleColor]::DarkGreen
OptionSeparator = " "
}
$menu.SetOptions($options)
# Trigger the menu and receive the user answer
$answer = $menu.GetAnswer()
Write-Host "The user answered: $answer"
# Import the module
Import-Module InteractiveMenu
# Define the items for the menu
$answerItems = @(
Get-InteractiveChooseMenuOption `
-Label "I choose good" `
-Value "good" `
-Info "Good info"
Get-InteractiveChooseMenuOption `
-Label "I choose bad" `
-Value "bad" `
-Info "Bad info"
)
# [Optional] You can change the colors and the symbols
$options = @{
MenuInfoColor = [ConsoleColor]::DarkYellow
QuestionColor = [ConsoleColor]::Magenta
HelpColor = [ConsoleColor]::Cyan
ErrorColor = [ConsoleColor]::DarkRed
HighlightColor = [ConsoleColor]::DarkGreen
OptionSeparator = " "
}
# Define the question of the menu
$question = "Choos between good and bad"
# Trigger the menu and receive the user answer
# Note: the options parameter is optional
$answer = Get-InteractiveMenuChooseUserSelection -Question $question -Answers $answerItems -Options $options
InteractiveMenuChooseMenuItem
InteractiveMenuChooseMenuItem([string]$label, [string]$value, [string]$info)
[string]$Label
- The text that you want to show for your option[string]$Value
- The value that you want returned if the user selects it[string]$Info
- The information about the option. This information is displayed under the optionsInteractiveMenuChooseMenu
InteractiveMenuChooseMenu([string]$question, [InteractiveMenuChooseMenuItem[]]$options)
[string]$question
- The question that you want to show above the options[InteractiveMenuChooseMenuItem[]]$options
- The list of answers to display in the menu[string] GetAnswer()
- Execute this method to trigger the menu and get the answer that the user selected[void] SetOptions([hashtable]$options)
- Execute this method to alter the options of the menu (more details in the Options section)MenuInfoColor
- Color of the item information. Default: [ConsoleColor]::DarkYellow
QuestionColor
- Color of the question text. Default: [ConsoleColor]::Magenta
HelpColor
- Color of the help items. Default: [ConsoleColor]::Cyan
ErrorColor
- Color of the errors. Default: [ConsoleColor]::DarkRed
HighlightColor
- Color of the current selected item. Default: [ConsoleColor]::DarkGreen
OptionSeparator
- The separator between the answers. Default: “spacespacespacespacespacespace” (6 spaces)