New-ConsoleMenuItem
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
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 @menuOptionsBuild 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 @menuOptionsConfirm an action and return a result record
Example 3
Get-ConsoleDemo -Slug 'toolbox' | Select-Object -ExpandProperty Source
Get-ConsoleDemo -Slug 'fleet' | Select-Object -ExpandProperty SourceRead 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
- The action, run on the pipeline thread; $MenuContext (also $_) offers Toast, Confirm, Ask, Run (a live dashboard) and ShowResult.
- -Badge Object
- Right-aligned text: a string, rich text, or a scriptblock recomputed whenever the menu draws.
- -Description String
- A sentence shown with the item.
- -Detail Object
- State text ("24 selected"): a string or a scriptblock recomputed whenever the menu draws.
- -DisabledReason Object
- Why the item is unavailable: a string, or a scriptblock returning the reason (nothing when available).
- -Hotkey Char
- The key that opens the item directly.
- -Icon Icon
- 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
- Literal text shown for this selectable menu item; use Description for its supporting detail.
- -Mark String
- A short mark before the title, as free text. Prefer -Icon, which follows the glyph tier.
- A submenu: a menu from New-ConsoleMenu, or a scriptblock of New-ConsoleMenuItem calls.
- -Form Form
- A form from New-ConsoleForm.
- -OnSubmit ScriptBlock
- Runs when the form is submitted; $Values is a hashtable of the values, $FormResult the full result.
- -ChoiceBadge Object
- 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
- 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
- A sentence for the focused choice: a property name or a scriptblock.
- -Display Object
- What to show for a choice: a property name or a scriptblock.
- -GroupBy Object
- The group of a choice: a property name or a scriptblock.
- -OnChange ScriptBlock
- Runs when the selection changes ($MenuContext and $_).
- -Preview ScriptBlock
- Preview content for the focused choice ($_ is the choice).
- -Select Object[]
- Choices for picking one value (any objects).
- -Selected Object
- 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
- The most values of a multi-selection.
- -Minimum Int32
- The fewest values of a multi-selection.
- -MultiSelect Object[]
- Choices for picking several values (any objects).
- -ArgumentList Object[]
- Arguments for the -LoadChoices script.
- -LoadChoices ScriptBlock
- Loads the choices in its own runspace (a spinner shows meanwhile). Pair with -Single for one value.
- -Single SwitchParameter
- With -LoadChoices: pick one value instead of several.
- -Page ScriptBlock
- Page content, built each time it shows: text, rich text, widgets, or objects shown as a list.
- -Review ScriptBlock
- 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
- 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
- Makes a divider (with -Label as its heading, if given).
- -Exit SwitchParameter
- 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.