Skip to content

Simple powershell menu to render interactive console menu

License

Notifications You must be signed in to change notification settings

admins-little-helper/PSMenu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PSMenu

Simple module to generate interactive console menus (like yeoman)

Examples

Very basic example.

Show-Menu @("option 1", "option 2", "option 3")

Basic example

Custom formatting

Custom formatting of menu items and multi-selection:

Show-Menu -MenuItems $(Get-NetAdapter) -MultiSelect -MenuItemFormatter { $Args | Select -Exp Name }

Classes as options (complex example)

Custom options

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.

Custom formatters and multiselect

Separators

Show-Menu @("Option A", "Option B", $(Get-MenuSeparator), "Quit")

Separators are unselectable items used for visual distinction in the menu.

Separator support

Callback

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)
}

Callback example

Installation

You can install it from the PowerShellGallery using PowerShellGet

Install-Module PSMenu

Features

  • Returns value of selected menu item
  • Returns index of selected menu item (using -ReturnIndex switch)
  • Multi-selection support (using -MultiSelect switch), use spacebar 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)

Documentation

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.

Contributing

Pull requests are very welcome!

About

Simple powershell menu to render interactive console menu

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 100.0%