WhatIf and Confirm support
📖 Scenario: You are creating a PowerShell script to safely delete files. You want to add safety features so users can see what will happen before files are deleted and confirm the action.
🎯 Goal: Build a PowerShell script that supports -WhatIf and -Confirm parameters to safely delete files.
📋 What You'll Learn
Create a function called
Remove-MyFile that deletes a file.Add
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] to enable -WhatIf and -Confirm support.Use
$PSCmdlet.ShouldProcess() to check if the action should proceed.Use
Write-Verbose to show a message when deleting a file.Test the function with
-WhatIf and -Confirm parameters.💡 Why This Matters
🌍 Real World
PowerShell scripts often delete files or make changes that can be risky. Using WhatIf and Confirm lets users preview or approve actions to avoid mistakes.
💼 Career
Many IT and automation jobs require writing safe scripts that support WhatIf and Confirm to prevent accidental data loss.
Progress0 / 4 steps