Bird
Raised Fist0
PowerShellscripting~10 mins

WhatIf and Confirm support in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - WhatIf and Confirm support
Start Script
Check -WhatIf?
YesShow WhatIf message, skip action
No
Check -Confirm?
YesAsk user to confirm
User says No?
YesSkip action
Perform action
End Script
The script first checks if -WhatIf is used to show the action without running it. If not, it checks -Confirm to ask user permission before running the action.
Execution Sample
PowerShell
Remove-Item -Path 'test.txt' -WhatIf
Remove-Item -Path 'test.txt' -Confirm
Shows how Remove-Item behaves with -WhatIf (shows message only) and -Confirm (asks user before deleting).
Execution Table
StepParameterActionUser InputResultOutput
1-WhatIfCheck -WhatIf parameterN/ATrueWhat if: Performing Remove-Item on 'test.txt'
2-WhatIfSkip actual Remove-ItemN/ANo deletionWhat if: Performing Remove-Item on 'test.txt'
3-ConfirmCheck -Confirm parameterN/ATruePrompt user: "Are you sure you want to perform this action?"
4-ConfirmUser inputNoAction cancelledRemove-Item not performed
5-ConfirmUser inputYesAction performedFile 'test.txt' deleted
6No parameterPerform Remove-Item directlyN/AAction performedFile 'test.txt' deleted
💡 Execution stops after action is skipped or performed based on parameters and user input.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4/5Final
WhatIfFalseTrueTrueTrueTrue
ConfirmFalseFalseTrueUser input (Yes/No)Depends on input
ActionPerformedFalseFalseFalseFalse or TrueTrue if confirmed or no params
Key Moments - 3 Insights
Why does the script not delete the file when -WhatIf is used?
Because at Step 1 and 2 in the execution_table, -WhatIf is True, so the script only shows the message and skips the actual deletion.
What happens if the user answers 'No' to the confirmation prompt?
At Step 4, the user input is 'No', so the action is cancelled and the file is not deleted, as shown in the execution_table.
If neither -WhatIf nor -Confirm is used, what does the script do?
At Step 6, without parameters, the script performs the action directly and deletes the file.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 2 when -WhatIf is used?
A"File 'test.txt' deleted"
B"What if: Performing Remove-Item on 'test.txt'"
C"Remove-Item not performed"
D"Prompt user: Are you sure you want to perform this action?"
💡 Hint
Check the Output column at Step 2 in the execution_table.
At which step does the user input affect whether the action runs or not?
AStep 1
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the User Input and Result columns in the execution_table.
If the user answers 'Yes' to the confirmation prompt, what is the final state of ActionPerformed?
ATrue
BDepends on -WhatIf
CFalse
DUnknown
💡 Hint
Check variable_tracker for ActionPerformed after Step 5.
Concept Snapshot
PowerShell supports -WhatIf and -Confirm parameters.
-WhatIf shows what would happen without running the action.
-Confirm asks user permission before running.
If -WhatIf is used, action is skipped but message shown.
If -Confirm is used, user input controls action.
Without these, action runs directly.
Full Transcript
This visual execution trace shows how PowerShell commands support -WhatIf and -Confirm parameters. When -WhatIf is used, the script shows a message about the action but does not perform it. When -Confirm is used, the script asks the user to confirm before running the action. If the user says no, the action is skipped. If yes, the action runs. Without these parameters, the action runs immediately. The execution table traces each step, showing parameter checks, user input, and results. The variable tracker shows how WhatIf, Confirm, and ActionPerformed change during execution. Key moments clarify common confusions about skipping actions and user confirmation. The quiz tests understanding of outputs and decision points. This helps beginners see exactly how these parameters control script behavior.

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

  1. Step 1: Understand the purpose of -WhatIf

    The -WhatIf parameter simulates the command's effect without making changes.
  2. 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.
  3. Final Answer:

    It shows what the command would do without actually performing the action. -> Option D
  4. 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

  1. Step 1: Recall how to enable Confirm support

    PowerShell functions use [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] to support -WhatIf and -Confirm.
  2. Step 2: Check each option

    Only Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the function. correctly shows the attribute syntax to enable confirmation support.
  3. Final Answer:

    Add [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')] above the function. -> Option A
  4. 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

  1. Step 1: Understand the function's use of ShouldProcess

    The function calls $PSCmdlet.ShouldProcess($Path), which respects -WhatIf and -Confirm.
  2. 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.
  3. Final Answer:

    It shows a message about deleting 'test.txt' but does not delete it. -> Option A
  4. 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

  1. Step 1: Check function attributes for Confirm support

    The function is missing [CmdletBinding(SupportsShouldProcess=$true)], which enables -Confirm and -WhatIf support.
  2. Step 2: Understand effect of missing attribute

    Without this attribute, the function ignores -Confirm and does not prompt.
  3. Final Answer:

    The function lacks [CmdletBinding(SupportsShouldProcess=$true)] attribute. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')] with $PSCmdlet.ShouldProcess -> Option C
  4. Quick Check:

    CmdletBinding + ShouldProcess = A [OK]
Hint: Use CmdletBinding and ShouldProcess for full Confirm and WhatIf [OK]
Common Mistakes:
  • Forgetting CmdletBinding attribute
  • Manually adding Confirm switch parameter
  • Always using -WhatIf inside function