PowerShell / Editorial solution guide

A WhatIf preview still changes state

Find an effect outside the ShouldProcess guard and verify a safe preview with an isolated fixture.

About 3 min readContent reviewed

Read-only knowledge base · No public comments

Recognize the symptoms

A command invoked with WhatIf changes a file, remote resource, or in-memory value despite showing preview output. Preserve a minimal synthetic reproduction and identify the exact effect before making further changes. Do not rerun a potentially destructive command against a real target merely to diagnose the issue.

Understand the likely causes

  • SupportsShouldProcess provides common parameters, but the function must call ShouldProcess and put the actual effect inside its approved branch. Preparatory work or a helper can still mutate state outside that branch.

Locate every effect

Trace the public function and its helpers using an owned test fixture. List assignments, file writes, remote calls, and other operations that can change state, including preparation and cleanup. Identify the target and action reported to ShouldProcess and compare them with the actual operation. A preview message alone is insufficient evidence that every mutation is guarded.

Move the mutation behind the decision

The sample function changes only an in-memory demonstration variable. Its assignment occurs only when ShouldProcess approves the action. With WhatIf, the variable remains original. For a real command, guard each relevant effect and make target and action descriptions accurate. Do not silently perform a preparatory write before the decision; redesign it or document and separately guard that effect.

powershell Guard a synthetic mutation
function Set-DemoState {
  [CmdletBinding(SupportsShouldProcess)]
  param([Parameter(Mandatory)][string]$Value)
  if ($PSCmdlet.ShouldProcess("demo state", "Set value")) {
    $script:DemoState = $Value
  }
}
$script:DemoState = "original"
Set-DemoState -Value "changed" -WhatIf
$script:DemoState

The assignment is inside the ShouldProcess branch, so the WhatIf invocation previews it without changing the variable.

Handle validation and partial failure

Validate the input and target before attempting the action, and enforce permissions at the system owning the resource. ShouldProcess is not an authorization check or a transaction. If a multi-step action fails after an earlier step succeeds, apply a documented recovery plan and report the partial outcome. Avoid logging credentials while collecting diagnostics, and never infer that a preview guarantees later execution will succeed.

Verify preview and execution separately

In a fresh test session initialize the fixture, run the function with WhatIf, and assert that the variable is unchanged. Run the actual action on the owned fixture and assert the intended value. Add invalid input and an injected failure to verify error propagation and unchanged unrelated state. If testing a real provider, use a temporary resource and the intended PowerShell edition and platform.

Verify the fix

Check the fixture before and after preview, then verify the real action only on the owned fixture. Confirm every helper’s effects are covered and errors remain visible.

Keep it from recurring

  • Review every effect, including preparation and cleanup, when adding SupportsShouldProcess. Retain preview and partial-failure regression tests and document authorization and recovery separately.

Official references