Skip to content
ConsoleMonster
Cmdlet

New-ConsoleMenuItem

Module
ConsoleMonster
Outputs
ConsoleMonster.Interactive.MenuItem

Creates a menu item: an action, a submenu, a form, a selection, a page, a review, the presets list, a separator or the exit.

Syntax

New-ConsoleMenuItem
[[-Action] <ScriptBlock>]
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
[<CommonParameters>]
#
Parameter set: Action
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
-Submenu <Object>
[<CommonParameters>]
#
Parameter set: Submenu
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
-Form <Form>
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
[-OnSubmit <ScriptBlock>]
[<CommonParameters>]
#
Parameter set: Form
New-ConsoleMenuItem
[-Badge <Object>]
[-ChoiceBadge <Object>]
[-ChoiceDisabledReason <Object>]
[-Describe <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Display <Object>]
[-GroupBy <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
[-OnChange <ScriptBlock>]
[-Preview <ScriptBlock>]
-Select <Object[]>
[-Selected <Object>]
[<CommonParameters>]
#
Parameter set: Select
New-ConsoleMenuItem
[-Badge <Object>]
[-ChoiceBadge <Object>]
[-ChoiceDisabledReason <Object>]
[-Describe <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Display <Object>]
[-GroupBy <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
[-Maximum <Int32>]
[-Minimum <Int32>]
-MultiSelect <Object[]>
[-OnChange <ScriptBlock>]
[-Preview <ScriptBlock>]
[-Selected <Object>]
[<CommonParameters>]
#
Parameter set: MultiSelect
New-ConsoleMenuItem
[-ArgumentList <Object[]>]
[-Badge <Object>]
[-ChoiceBadge <Object>]
[-ChoiceDisabledReason <Object>]
[-Describe <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Display <Object>]
[-GroupBy <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
-LoadChoices <ScriptBlock>
[-Mark <String>]
[-Maximum <Int32>]
[-Minimum <Int32>]
[-OnChange <ScriptBlock>]
[-Preview <ScriptBlock>]
[-Selected <Object>]
[-Single]
[<CommonParameters>]
#
Parameter set: LoadChoices
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
-Page <ScriptBlock>
[<CommonParameters>]
#
Parameter set: Page
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
-Review <ScriptBlock>
[<CommonParameters>]
#
Parameter set: Review
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
-Presets
[<CommonParameters>]
#
Parameter set: Presets
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
-Separator
[<CommonParameters>]
#
Parameter set: Separator
New-ConsoleMenuItem
[-Badge <Object>]
[-Description <String>]
[-Detail <Object>]
[-DisabledReason <Object>]
-Exit
[-Hotkey <Char>]
[-Icon <Icon>]
[[-Label] <String>]
[-Mark <String>]
[<CommonParameters>]
#
Parameter set: Exit

Description

What the item does follows from the parameter you give: -Action (a scriptblock, run on the pipeline thread with $MenuContext), -Submenu (a menu or a scriptblock of items), -Form (with -OnSubmit, which gets $Values and $FormResult), -Select or -MultiSelect (the chosen objects stay on the item: $item.Selection), -LoadChoices (loaded in its own runspace with a spinner), -Page (content built each time it shows), -Review (a summary with the equivalent command and Run), -Presets, -Separator or -Exit. -Badge and -Detail can be scriptblocks, recomputed every time the menu draws; -DisabledReason returns why the item is unavailable.

Examples

Example 1

$tasks = @(
    [pscustomobject]@{ Name = 'Backup'; Category = 'Files'; Description = 'Preview a backup'; Enabled = $true }
    [pscustomobject]@{ Name = 'Verify'; Category = 'Files'; Description = 'Verify a backup'; Enabled = $false }
)
$menuOptions2 = @{
    Title = 'Tasks'
    Content = {
        $menuItemOptions = @{
            Label = 'Tasks'
            Hotkey = 'k'
            MultiSelect = $tasks
            Display = 'Name'
            GroupBy = 'Category'
            Preview = { $_.Description }
            Selected = { $_.Enabled }
        }
        New-ConsoleMenuItem @menuItemOptions
        New-ConsoleMenuItem -Exit
    }
}
$menu = New-ConsoleMenu @menuOptions2
$menuOptions = @{
    Menu = $menu
}
Show-ConsoleMenu @menuOptions

Build a multiple-choice item with an initial selection

Example 2

$menuOptions2 = @{
    Title = 'Inventory'
    Content = {
        $menuItemOptions = @{
            Label = 'Preview refresh'
            Action = {
                if ($MenuContext.Confirm('Preview an inventory refresh?')) {
                    $MenuContext.Toast('Preview ready')
                    $MenuContext.ShowResult(@{ Title = 'Refresh preview'; Status = 'Pass'; Summary = 'No inventory changes were made.' })
                }
            }
        }
        New-ConsoleMenuItem @menuItemOptions
        New-ConsoleMenuItem -Exit
    }
}
$menu = New-ConsoleMenu @menuOptions2
$menuOptions = @{
    Menu = $menu
}
Show-ConsoleMenu @menuOptions

Confirm an action and return a result record

Example 3

Get-ConsoleDemo -Slug 'toolbox' | Select-Object -ExpandProperty Source
Get-ConsoleDemo -Slug 'fleet' | Select-Object -ExpandProperty Source

Read any of these gallery scripts to see New-ConsoleMenuItem in a complete example. Each lookup returns the script's source. Use the same slug with Show-ConsoleDemo to run it.

Parameters

-Action ScriptBlock optionalposition: 1sets: Action
The action, run on the pipeline thread; $MenuContext (also $_) offers Toast, Confirm, Ask, Run (a live dashboard) and ShowResult.
-Badge Object optional
Right-aligned text: a string, rich text, or a scriptblock recomputed whenever the menu draws.
-Description String optional
A sentence shown with the item.
-Detail Object optional
State text ("24 selected"): a string or a scriptblock recomputed whenever the menu draws.
-DisabledReason Object optional
Why the item is unavailable: a string, or a scriptblock returning the reason (nothing when available).
-Hotkey Char optional
The key that opens the item directly.
-Icon Icon optionalaliases: IconName
An icon catalog key that follows the glyph tier: item, targets, tasks, settings, favorite, run, back, list, help, warning, done or exit (for example ● in modern fonts, o in ASCII). Takes precedence over -Mark.
-Label String optionalposition: 0
Literal text shown for this selectable menu item; use Description for its supporting detail.
-Mark String optional
A short mark before the title, as free text. Prefer -Icon, which follows the glyph tier.
-Submenu Object requiredsets: Submenu
A submenu: a menu from New-ConsoleMenu, or a scriptblock of New-ConsoleMenuItem calls.
-Form Form requiredsets: Form
A form from New-ConsoleForm.
-OnSubmit ScriptBlock optionalsets: Form
Runs when the form is submitted; $Values is a hashtable of the values, $FormResult the full result.
-ChoiceBadge Object optionalsets: Select, MultiSelect, LoadChoices
Badge for each selection choice: a property name or scriptblock. With -LoadChoices, use a property name from the loader's output. The item's own badge remains -Badge.
-ChoiceDisabledReason Object optionalsets: Select, MultiSelect, LoadChoices
Disabled reason for each selection choice: a property name or scriptblock; null or empty enables the choice. With -LoadChoices, use a property name. The item's own disabled reason remains -DisabledReason.
-Describe Object optionalsets: Select, MultiSelect, LoadChoices
A sentence for the focused choice: a property name or a scriptblock.
-Display Object optionalsets: Select, MultiSelect, LoadChoices
What to show for a choice: a property name or a scriptblock.
-GroupBy Object optionalsets: Select, MultiSelect, LoadChoices
The group of a choice: a property name or a scriptblock.
-OnChange ScriptBlock optionalsets: Select, MultiSelect, LoadChoices
Runs when the selection changes ($MenuContext and $_).
-Preview ScriptBlock optionalsets: Select, MultiSelect, LoadChoices
Preview content for the focused choice ($_ is the choice).
-Select Object[] requiredsets: Select
Choices for picking one value (any objects).
-Selected Object optionalsets: Select, MultiSelect, LoadChoices
Pre-selected choices: the objects or their texts, or a scriptblock returning $true for static choices. With -LoadChoices, pass initial values; they are matched when the choices arrive.
-Maximum Int32 optionalsets: MultiSelect, LoadChoices
The most values of a multi-selection.
-Minimum Int32 optionalsets: MultiSelect, LoadChoices
The fewest values of a multi-selection.
-MultiSelect Object[] requiredsets: MultiSelect
Choices for picking several values (any objects).
-ArgumentList Object[] optionalsets: LoadChoices
Arguments for the -LoadChoices script.
-LoadChoices ScriptBlock requiredsets: LoadChoices
Loads the choices in its own runspace (a spinner shows meanwhile). Pair with -Single for one value.
-Single SwitchParameter optionalsets: LoadChoices
With -LoadChoices: pick one value instead of several.
-Page ScriptBlock requiredsets: Page
Page content, built each time it shows: text, rich text, widgets, or objects shown as a list.
-Review ScriptBlock requiredsets: Review
A review built each time it shows: a hashtable with Rows (an ordered hashtable of label = value), Command (the equivalent command line), Problems (strings that block running), Run (a scriptblock) and optionally Title, Description and RunText.
-Presets SwitchParameter requiredsets: Presets
Makes the item list the menu's presets: the ones it comes with (New-ConsoleMenu -Preset) first, marked as built in, with their previews, then the saved ones. Enter applies one; Delete removes a saved one (Shell and Classic layouts; the Inline layout applies them only).
-Separator SwitchParameter requiredsets: Separator
Makes a divider (with -Label as its heading, if given).
-Exit SwitchParameter requiredsets: Exit
Makes the exit item (-Label defaults to Exit).

Common Parameters

This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

For more information, see about_CommonParameters.