Get-LJKShouldProcessStatus: Inspecting ShouldProcess Decisions in PowerShell
PowerShell functions that support -WhatIf
and -Confirm
rely on user input or session preferences to determine whether to perform an action. But what if your function needs to know what the user decided in a parent function or caller?
This is where Get-LJKShouldProcessStatus
comes in.
Why This Is Useful
If you’ve ever written a wrapper, proxy command, or multi-layered function pipeline in PowerShell 7+, you’ve likely run into the challenge of:
- Needing to react to the user’s -WhatIf or -Confirm decision
- Having to pass confirmation status downstream to helper functions
- Wanting to introspect how a higher-level command handled ShouldProcess
By default, PowerShell does not expose a public method to retrieve this information. This function bridges that gap-albeit with some clever reflection.
Core Behavior
The function looks at the internal CommandRuntime
object associated with a PSCmdlet
, and inspects a non-public field called lastShouldProcessContinueStatus
.
It works in one of two ways:
- Automatically resolving the calling scope’s
PSCmdlet
object - Accepting a
CmdletObject
explicitly passed in
You can also specify the depth of scope to inspect with the -Scope
parameter.
Usage Examples
Basic (call from a function)
# From a nested function, get the most recent decision
function Test-ConfirmFlow
{
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
param()
process
{
if ($PSCmdlet.ShouldProcess("target", "do the thing"))
{
# do the work
Write-Output "Action performed."
}
else
{
Write-Output "Action skipped."
}
# inspect the runtime decision from this function
$status = Get-LJKShouldProcessStatus -CmdletObject $PSCmdlet
"Last decision: $status"
}
}
From a helper deep in the call stack
If you call Get-LJKShouldProcessStatus from a nested helper, set the -Scope parameter to walk up scopes:
# Look two scopes up, such as from a helper called by another function
Get-LJKShouldProcessStatus -Scope 2
Passing a custom cmdlet object
If you have a PSCmdlet-derived object on hand:
# Pass in a specific Cmdlet object to inspect
Get-LJKShouldProcessStatus -CmdletObject $MyCmdlet
What the function returns
The function returns whatever value the runtime stores in its internal lastShouldProcessContinueStatus field. In practice you will most often see:
Yes
- user accepted / allowed the actionNo
- user declined / suppressed the actionNoToAll
- user declined all future actions in this contextYesToAll
- user accepted all future actions in this context$null
- no decision was recorded (e.g., no prior ShouldProcess/ShouldContinue call) or user cancelledctrl+c
Note: exact shape / type may vary by PowerShell version; treat returned value conservatively.
Developer Notes
Caution
This function uses reflection to access non-public members, which may break in future PowerShell versions. Use with caution and test when upgrading PowerShell.
- It is primarily intended for advanced scripting scenarios where you need to build meta-cmdlets or complex function pipelines.
- Always ensure your functions properly declare
SupportsShouldProcess
in their[CmdletBinding()]
attribute to enable this functionality.
When To Use It
This function is especially handy if you’re:
- Writing advanced cmdlet wrappers
- Building workflow orchestrators in PowerShell
- Designing meta-cmdlets that defer to other commands
It’s a power tool-use it wisely.