Bird
0
0

You wrote this function:

medium📝 Debug Q14 of 15
PowerShell - Scripting Best Practices
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?
AYou must add <code>-WhatIf</code> to enable confirmation.
BThe function lacks <code>[CmdletBinding(SupportsShouldProcess=$true)]</code> attribute.
CThe <code>param</code> block is missing <code>[switch]$Confirm</code> parameter.
DRemove-Item does not support confirmation.
Step-by-Step Solution
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]
Quick Trick: Add CmdletBinding to enable -Confirm prompts [OK]
Common Mistakes:
  • Thinking -WhatIf enables confirmation
  • Adding Confirm parameter manually
  • Assuming Remove-Item disables confirmation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes