Using [CmdletBinding()] to Add -WhatIf and -Confirm to PowerShell Functions
Learn how to add -WhatIf and -Confirm support to PowerShell scripts by turning them into advanced functions with [CmdletBinding()] and $PSCmdlet.ShouldProcess.
20 Nov 2025, 11:34 UTC
![Using [CmdletBinding()] to Add -WhatIf and -Confirm to PowerShell Functions](/_next/image?url=%2Fmedia%2Fgenerated%2F7345c470-c184-4b7b-a4a9-64339b2dc51f.webp&w=3840&q=75)
Add -WhatIf and -Confirm to a PowerShell function
When you need a script that behaves like a built‑in cmdlet, turn it into an advanced function by adding the [CmdletBinding()] attribute. This gives the function automatic access to PowerShell’s common parameters (-WhatIf, -Confirm, -Verbose, -Debug, -ErrorAction) without writing extra code.
Worked example
function Invoke-DemoAction {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Target
)
# The action only runs when ShouldProcess returns $true
if ($PSCmdlet.ShouldProcess($Target, 'Perform action')) {
Write-Verbose "Starting action on $Target"
# Replace the line below with the real work you want to do
# Example: Remove-Item -Path $Target -Force
Write-Output "Action completed on $Target"
}
}
How to verify the behavior
-
Run the function with
-WhatIfand-Verbose:Invoke-DemoAction -Target 'C:\Temp\test.txt' -WhatIf -VerboseYou should see a verbose message ("Starting action on …") followed by a line that describes what would happen (e.g., "What if: Performing the operation 'Perform action' on target 'C:\Temp\test.txt'"). No file is created or changed.
-
Run the same command without
-WhatIfon a safe target:Invoke-DemoAction -Target 'C:\Temp\test.txt' -VerboseIf the file exists, the function writes the verbose message and then outputs "Action completed on …". If you replaced the placeholder with a real command (e.g.,
Remove-Item), the file would be removed. If nothing happens, check that theShouldProcesscall surrounds the action and that the$Targetvalue matches the argument.
Limits and common mistakes
- Missing ShouldProcess: If you place the action outside the
if ($PSCmdlet.ShouldProcess(...))block,-WhatIfand-Confirmare ignored and the action always runs. - Using Write-Host: Writing directly to the host bypasses PowerShell’s output streams, so
-Verboseand-Debugmessages are suppressed and debugging becomes harder. - Confirm impact: The default impact level is
Medium. If you want-Confirmto prompt only for high‑risk actions, set[CmdletBinding(ConfirmImpact='High')]and match the impact in yourShouldProcesscall. - Version requirement: Advanced functions need PowerShell 2.0 or later; they work in Windows PowerShell 5.1, PowerShell 7.x, and PowerShell Core.
- Plain scripts: A script file that lacks the
[CmdletBinding()]attribute cannot expose the common parameters; you must wrap the code in a function as shown.
Practical check
# Test WhatIf
Invoke-DemoAction -Target 'C:\Temp\demo.txt' -WhatIf -Verbose
# Test actual execution (use a disposable file)
New-Item -Path 'C:\Temp\demo.txt' -ItemType File -Force | Out-Null
Invoke-DemoAction -Target 'C:\Temp\demo.txt' -Verbose
# Clean up
Remove-Item -Path 'C:\Temp\demo.txt' -Force
If the first call shows a WhatIf message and the second call reports the action completed, the advanced function is working correctly. Adjust the placeholder inside the ShouldProcess block to perform your real task.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.