0
0
PowerShellscripting~10 mins

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

Choose your learning style9 modes available
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.