Bird
0
0

In a PowerShell function with SupportsShouldProcess enabled, which is the correct way to check if the action should proceed?

easy📝 Syntax Q3 of 15
PowerShell - Scripting Best Practices
In a PowerShell function with SupportsShouldProcess enabled, which is the correct way to check if the action should proceed?
Aif ($PSCmdlet.ShouldProcess($Target)) { ... }
Bif ($ConfirmPreference) { ... }
Cif ($WhatIfPreference) { ... }
Dif ($PSCmdlet.Confirm) { ... }
Step-by-Step Solution
Solution:
  1. Step 1: Use ShouldProcess method

    The correct method to check if the action should proceed is $PSCmdlet.ShouldProcess() passing the target resource.
  2. Step 2: Understand other options

    $ConfirmPreference and $WhatIfPreference are preference variables, not methods to check action.
  3. Final Answer:

    if ($PSCmdlet.ShouldProcess($Target)) { ... } -> Option A
  4. Quick Check:

    Use ShouldProcess method to confirm action [OK]
Quick Trick: ShouldProcess method checks if action proceeds [OK]
Common Mistakes:
  • Using preference variables instead of ShouldProcess
  • Assuming $PSCmdlet.Confirm exists
  • Confusing WhatIfPreference with action check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes