Simple module to generate interactive console menus (like yeoman)
Very basic example.
Show-Menu @("option 1", "option 2", "option 3")
Custom formatting of menu items and multi-selection:
Show-Menu -MenuItems $(Get-NetAdapter) -MultiSelect -MenuItemFormatter { $Args | Select -Exp Name }
You can also use custom options (enriched options). A more complicated example:
class MyMenuOption {
[String]$DisplayName
[ScriptBlock]$Script
[String]ToString() {
Return $This.DisplayName
}
}
function New-MenuItem([String]$DisplayName, [ScriptBlock]$Script) {
$MenuItem = [MyMenuOption]::new()
$MenuItem.DisplayName = $DisplayName
$MenuItem.Script = $Script
Return $MenuItem
}
$Opts = @(
$(New-MenuItem -DisplayName "Say Hello" -Script { Write-Host "Hello!" }),
$(New-MenuItem -DisplayName "Say Bye!" -Script { Write-Host "Bye!" })
)
$Chosen = Show-Menu -MenuItems $Opts
& $Chosen.Script
This will show the menu items like you expect.
Show-Menu @("Option A", "Option B", $(Get-MenuSeparator), "Quit")
Separators are unselectable items used for visual distinction in the menu.
The Callback option can be used to perform actions while the menu is displayed. Note: always save & restore the cursor position like in the following example if the host output is changed in the callback.
Clear-Host
Write-Host "Current time: $(Get-Date)"
Write-Host ""
Show-Menu @("Option A", "Option B") -Callback {
$lastTop = [Console]::CursorTop
[System.Console]::SetCursorPosition(0, 0)
Write-Host "Current time: $(Get-Date)"
[System.Console]::SetCursorPosition(0, $lastTop)
}
You can install it from the PowerShellGallery using PowerShellGet
Install-Module PSMenu
- Returns value of selected menu item
- Returns index of selected menu item (using
-ReturnIndex
switch) - Multi-selection support (using
-MultiSelect
switch), usespacebar
to select items - Navigation with
up/down/page-up/page-down/home/end
keys - Longer list scroll within window
- Support for separators
- Esc key quits the menu (
$null
returned) - Extensively documented
- Perform actions while the menu is displayed (using
-Callback
)
For details, check out the comment-based help in the source code,
or use Get-Help Show-Menu
from the command-line. See also Get-Help
.
- Source hosted at GitHub
- Report issues/questions/feature requests on GitHub Issues
Pull requests are very welcome!