New-ConsoleCommand
Defines an in-app action for buttons, palettes and grid rows.
Syntax
New-ConsoleCommand
[-ArgumentList <Object[]>]
[-ConfirmPrompt <String>]
[-ImportModule <String[]>]
[-Key] <String>
[-Label <String>]
-ScriptBlock <ScriptBlock>
[-Shortcut <String>]
[<CommonParameters>]#All Parameter SetsDescription
Creates a passive command definition. Work runs in an isolated runspace with explicit arguments and module imports, not the defining closure. The first script parameter receives a deserialized row snapshot. Selected or marked rows are captured before optional modal confirmation and run in order. Output, host messages, warnings, errors and progress stream into the app's log. F6 requests cancellation while the app stays open. After a two-second grace period the app becomes available even when a native call is still stopping. That command cannot start again until its previous runspace exits; other commands remain available. Cancellation cannot undo effects already performed by an external operation.
Examples
Example 1
$restart = New-ConsoleCommand -Key Restart -Label 'Restart service' -Shortcut Ctrl+R -ConfirmPrompt 'Restart selected services?' -ScriptBlock { param($Row) Restart-Service -Name $Row.Name -ErrorAction Stop }; Get-Service | Show-ConsoleGrid -Action $restartRestart selected services after confirmation without closing the viewer.
Parameters
- -ArgumentList Object[]
- Explicit values serialized when defining the command and passed after Row.
- -ConfirmPrompt String
- Optional modal confirmation text. {Selection} expands to the focused row label or marked-row count. No is the initial answer.
- -ImportModule String[]
- Modules to import in the operation's isolated runspace.
- -Key String
- Stable command identity.
- -Label String
- Visible command text; defaults to Key.
- -ScriptBlock ScriptBlock
- Operation source, receiving param($Row, ...explicit arguments). Closure variables are not inherited.
- -Shortcut String
- Optional key chord, for example Ctrl+R or F5.
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.