Skip to content

Lesson 10 of 13 · 15m

-WhatIf, Idempotency, and the Blast Radius

ShouldProcess makes dry-runs real, idempotency makes retries safe, and there is no transaction or undo.

Remove-Item C:\Users -Recurse -WhatIf prints what it would do without doing it. That parameter — and its sibling -Confirm — exists on nearly every state-changing cmdlet, and it's implemented by the cmdlet author calling $PSCmdlet.ShouldProcess("target", "action") before touching anything. Automation earns its -WhatIf support the same way: declare [CmdletBinding(SupportsShouldProcess)] and gate every mutating call behind if ($PSCmdlet.ShouldProcess($target, "restart service")).

The safety rails that aren't

-WhatIf is a convention, not a transaction. Cmdlets that support it check ShouldProcess and skip; cmdlets and native executables that don't support it do the work anyway. net.exe stop spooler -WhatIf stops the spooler — the extra argument is ignored. Invoke-CimMethod on a vendor class, raw [System.IO.File]::Delete(), ADSI calls — none of these honor -WhatIf either. The flag only protects you where someone explicitly opted in, which means the dry-run habit works for Remove-Item, Stop-Service, Set-*, New-*, Disable-* — and is silently void everywhere else.

PowerShell once shipped a real transaction system — Start-Transaction, [UseTransaction] parameters on registry paths — deprecated in Windows PowerShell 5.x and absent in PS7 entirely, because almost nothing ever supported it and nobody wanted to pay for full transactional rollback in a shell. The design consequence is real: there is no undo. Your compensating logic is your rollback — snapshot the state you mutate, write a reverse path, or scope the change narrowly enough that redo-from-scratch is the revert.

Idempotency is the actual discipline

The script that appends a config line on every run produces forty copies of the line by Christmas. The script that creates a queue if it doesn't exist can be run a thousand times. Automation must be idempotent — check the desired state, converge to it, emit whether a change was needed:

$line = 'net.ipv4.ip_forward=1'
$content = Get-Content /etc/sysctl.conf -ErrorAction Stop
if ($content -notcontains $line) {
    Add-Content /etc/sysctl.conf $line
    $changed = $true
}

This pattern is why DSC and configuration management exist — they're idempotency formalized. Without it, retry logic becomes corruption logic: a task that fails halfway and re-runs duplicates records, re-sends notifications, double-creates resources. Every scheduled job should be re-runnable in a loop while ($true) and converge — if the second run does anything new, the script has a bug, not a schedule.

ConfirmImpact and $ConfirmPreference

$ConfirmPreference defaults to High. Cmdlets tag themselves ConfirmImpact = 'High' (Stop-Computer, Restart-Computer) and get a prompt; Medium/Low don't prompt by default — Remove-Item is only Medium, and its famous recursion prompt is not a ShouldProcess confirm at all: it's a separate ShouldContinue call that -Confirm:$false and $ConfirmPreference don't touch (passing -Recurse is the consent it wants). Automation runs -Confirm:$false to say "I mean it" — and that's correct, because a -WhatIf dry run already happened in the pipeline above it. The footgun: Set-ADUser -Identity x -Enabled $false is ConfirmImpact High in some paths and not others; a $ConfirmPreference = 'None' set at session top is a global "yes to everything" that quietly disarms the prompt you wanted. Set it per call.

Fan-out is where blast radius lives

Invoke-Command -ComputerName $list on a $list you just Get-Contented has two failure modes that have both happened in prod: the file is empty and the command errors or — worse variants — the list includes * pattern-expansions or a . and a stray newline turns "web servers" into "everything reachable." Validate before fan-out: non-empty, dedupe (Sort-Object -Unique), hostname-shaped regex ^[a-z0-9.-]+$, and a count in the log. Then -ThrottleLimit — default 32 — is the concurrency blast radius: on a job against 500 boxes, the throttle is the difference between a controlled wave and a self-inflicted thundering herd. -ThrottleLimit 10 is the polite value.

The command flag that causes the most silent damage isn't a Remove-* — it's -Force. On Remove-Item it bypasses read-only and prompts. On Set-ADUser it doesn't exist. On Export-Csv it overwrites without asking. -Force means "I accept whatever happens" — and in automation, whatever happens is usually wrong. Prefer explicit -Recurse -ErrorAction Stop with the target validated.

The practice

Gate every mutation behind ShouldProcess so -WhatIf works as advertised; run the same command with -WhatIf before every production change — it's free and it's the only dry-run you get. Design scripts to be re-run-safe: check-then-set, log changed vs skipped, exit a code that says which. Validate server lists before fan-out, set -ThrottleLimit deliberately, and remember there is no transaction and no undo — the snapshot you took before mutating is the only rollback that exists.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.