Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
1
Create the Remove-MyFile function
Create a PowerShell function called Remove-MyFile that takes a parameter Path and deletes the file at that path using Remove-Item.
PowerShell
Hint
Use Remove-Item -Path $Path inside the function to delete the file.
2
Add CmdletBinding to support -WhatIf and -Confirm
Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the param block in the Remove-MyFile function to enable -WhatIf and -Confirm support.
PowerShell
Hint
Place [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] right before the function keyword.
3
Use $PSCmdlet.ShouldProcess() to confirm action
Inside the Remove-MyFile function, replace the direct Remove-Item call with a check using if ($PSCmdlet.ShouldProcess($Path, 'Remove file')). Only call Remove-Item if this returns true. Also add Write-Verbose to show a message before deleting.
PowerShell
Hint
Use if ($PSCmdlet.ShouldProcess($Path, 'Remove file')) { ... } to check before deleting.
4
Test the function with -WhatIf and -Confirm
Call Remove-MyFile with -Path set to "C:\temp\test.txt" and add the -WhatIf parameter to see what would happen. Then call it again with -Confirm to ask for confirmation.
PowerShell
Hint
Run Remove-MyFile -Path "C:\temp\test.txt" -WhatIf and Remove-MyFile -Path "C:\temp\test.txt" -Confirm to test.
Practice
(1/5)
1. What does the -WhatIf parameter do when used with a PowerShell command?
easy
A. It logs the command output to a file automatically.
B. It forces the command to run without any confirmation prompts.
C. It cancels the command immediately after starting.
D. It shows what the command would do without actually performing the action.
Solution
Step 1: Understand the purpose of -WhatIf
The -WhatIf parameter simulates the command's effect without making changes.
Step 2: Compare options to the definition
Only It shows what the command would do without actually performing the action. correctly describes this simulation behavior.
Final Answer:
It shows what the command would do without actually performing the action. -> Option D
Quick Check:
-WhatIf simulates action = D [OK]
Hint: WhatIf means "show only, don't do" [OK]
Common Mistakes:
Thinking -WhatIf runs the command fully
Confusing -WhatIf with -Confirm
Assuming it cancels the command
2. Which of the following is the correct way to add confirmation support to a PowerShell function?
easy
A. Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the function.
B. Add param([switch]$Confirm) inside the function body.
C. Use Write-Host 'Confirm?' inside the function.
D. Add -Confirm inside the function name.
Solution
Step 1: Recall how to enable Confirm support
PowerShell functions use [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] to support -WhatIf and -Confirm.
Step 2: Check each option
Only Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the function. correctly shows the attribute syntax to enable confirmation support.
Final Answer:
Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the function. -> Option A
Quick Check:
Enable Confirm with CmdletBinding attribute = C [OK]
Hint: Use CmdletBinding attribute to enable Confirm support [OK]
Common Mistakes:
Adding param switch instead of CmdletBinding
Using Write-Host for confirmation prompts
Trying to put -Confirm in function name
3. Given this function with Confirm support:
[CmdletBinding(SupportsShouldProcess=$true)]
function Remove-File {
param([string]$Path)
if ($PSCmdlet.ShouldProcess($Path)) {
Remove-Item $Path
}
}
What happens when you run Remove-File -Path 'test.txt' -WhatIf?
medium
A. It shows a message about deleting 'test.txt' but does not delete it.
B. It deletes 'test.txt' without asking.
C. It asks for confirmation before deleting 'test.txt'.
D. It throws an error because -WhatIf is not supported.
Solution
Step 1: Understand the function's use of ShouldProcess
The function calls $PSCmdlet.ShouldProcess($Path), which respects -WhatIf and -Confirm.
Step 2: Effect of running with -WhatIf
Using -WhatIf causes ShouldProcess to return false but show what would happen, so Remove-Item is not called.
Final Answer:
It shows a message about deleting 'test.txt' but does not delete it. -> Option A
Quick Check:
-WhatIf shows action, no delete = B [OK]
Hint: ShouldProcess respects -WhatIf to simulate actions [OK]
Common Mistakes:
Assuming -WhatIf deletes files
Confusing -WhatIf with -Confirm
Thinking function lacks support for -WhatIf
4. You wrote this function:
function Delete-Data {
param([string]$File)
if ($PSCmdlet.ShouldProcess($File)) {
Remove-Item $File
}
}
But when you run Delete-Data -File 'data.txt' -Confirm, it does not ask for confirmation. Why?
medium
A. You must add -WhatIf to enable confirmation.
B. The function lacks [CmdletBinding(SupportsShouldProcess=$true)] attribute.
C. The param block is missing [switch]$Confirm parameter.
D. Remove-Item does not support confirmation.
Solution
Step 1: Check function attributes for Confirm support
The function is missing [CmdletBinding(SupportsShouldProcess=$true)], which enables -Confirm and -WhatIf support.
Step 2: Understand effect of missing attribute
Without this attribute, the function ignores -Confirm and does not prompt.
Final Answer:
The function lacks [CmdletBinding(SupportsShouldProcess=$true)] attribute. -> Option B
Quick Check:
Missing CmdletBinding disables Confirm = A [OK]
Hint: Add CmdletBinding to enable -Confirm prompts [OK]
Common Mistakes:
Thinking -WhatIf enables confirmation
Adding Confirm parameter manually
Assuming Remove-Item disables confirmation
5. You want to write a PowerShell function that deletes files but only after confirmation, and also supports -WhatIf. Which code snippet correctly implements this?
hard
A. [CmdletBinding()]
function Remove-MyFile {
param([string]$Path)
Remove-Item $Path -Confirm
}
B. function Remove-MyFile {
param([string]$Path, [switch]$Confirm)
if ($Confirm) {
Remove-Item $Path
}
}
C. [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
function Remove-MyFile {
param([string]$Path)
if ($PSCmdlet.ShouldProcess($Path, 'Remove')) {
Remove-Item $Path
}
}
D. function Remove-MyFile {
param([string]$Path)
Remove-Item $Path -WhatIf
}
Solution
Step 1: Identify correct use of CmdletBinding and ShouldProcess
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
function Remove-MyFile {
param([string]$Path)
if ($PSCmdlet.ShouldProcess($Path, 'Remove')) {
Remove-Item $Path
}
} uses [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')] and calls $PSCmdlet.ShouldProcess, enabling both -Confirm and -WhatIf support.
Step 2: Check other options for missing or incorrect usage
function Remove-MyFile {
param([string]$Path, [switch]$Confirm)
if ($Confirm) {
Remove-Item $Path
}
} lacks CmdletBinding and misuses Confirm as a manual switch. [CmdletBinding()]
function Remove-MyFile {
param([string]$Path)
Remove-Item $Path -Confirm
} lacks SupportsShouldProcess and forces -Confirm on Remove-Item only. function Remove-MyFile {
param([string]$Path)
Remove-Item $Path -WhatIf
} always uses -WhatIf, which is not correct for actual deletion.
Final Answer:
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')] with $PSCmdlet.ShouldProcess -> Option C
Quick Check:
CmdletBinding + ShouldProcess = A [OK]
Hint: Use CmdletBinding and ShouldProcess for full Confirm and WhatIf [OK]