0
0
PowerShellscripting~5 mins

WhatIf and Confirm support in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the -WhatIf parameter do in PowerShell?
It shows what would happen if a command runs, without actually making any changes. It's like a preview or a dry run.
Click to reveal answer
beginner
What is the purpose of the -Confirm parameter in PowerShell?
It asks you to confirm before running a command that changes something. You get a yes/no question to avoid mistakes.
Click to reveal answer
intermediate
How do you add -WhatIf support to your own PowerShell function?
Add [CmdletBinding(SupportsShouldProcess=$true)] at the top and use $PSCmdlet.ShouldProcess() inside your function to check if it should run.
Click to reveal answer
beginner
What happens if you run a command with -Confirm and answer 'No'?
The command stops and does not make any changes. This helps prevent unwanted actions.
Click to reveal answer
beginner
Why is it good practice to support -WhatIf and -Confirm in scripts?
It helps users avoid mistakes by previewing changes and asking for confirmation, making scripts safer to run.
Click to reveal answer
What does the -WhatIf parameter do when added to a PowerShell command?
AShows what would happen without making changes
BAutomatically confirms the command
CRuns the command silently
DCancels the command immediately
Which attribute enables -WhatIf support in a PowerShell function?
A[CmdletBinding(SupportsShouldProcess=$true)]
B[Parameter(Mandatory=$true)]
C[OutputType([string])]
D[ValidateSet()]
What does the $PSCmdlet.ShouldProcess() method do inside a function?
AStops the script immediately
BPrints the command output
CChecks if the command should run based on <code>-WhatIf</code> or <code>-Confirm</code>
DRuns the command without confirmation
If you run a command with -Confirm and choose 'No', what happens?
AThe command runs anyway
BThe command runs silently
CThe command runs twice
DThe command does not run
Why should script authors add -WhatIf and -Confirm support?
ATo hide script output
BTo make scripts safer and prevent accidental changes
CTo make scripts run faster
DTo disable user interaction
Explain how to add WhatIf and Confirm support to a PowerShell function.
Think about the attribute and method that enable this support.
You got /3 concepts.
    Describe the difference between -WhatIf and -Confirm parameters in PowerShell.
    One previews, the other asks permission.
    You got /3 concepts.