Bird
0
0

You have this function:

medium📝 Debug Q6 of 15
PowerShell - Scripting Best Practices
You have this function:
function Remove-Log {
  param($File)
  if ($PSCmdlet.ShouldProcess($File)) {
    Remove-Item $File
  }
}

When running Remove-Log -File 'log.txt' -WhatIf, it throws an error. What is the likely cause?
ARemove-Item does not support -WhatIf
BThe function lacks the CmdletBinding attribute with SupportsShouldProcess enabled
CThe parameter $File is not declared as mandatory
DThe function is missing a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Check CmdletBinding attribute

    For $PSCmdlet.ShouldProcess to work properly and support -WhatIf, the function must have [CmdletBinding(SupportsShouldProcess=$true)].
  2. Step 2: Understand Remove-Item support

    Remove-Item supports -WhatIf natively, so that is not the issue.
  3. Step 3: Parameter and return statements

    Mandatory parameters or return statements do not affect -WhatIf support.
  4. Final Answer:

    The function lacks the CmdletBinding attribute with SupportsShouldProcess enabled -> Option B
  5. Quick Check:

    CmdletBinding with SupportsShouldProcess required [OK]
Quick Trick: CmdletBinding with SupportsShouldProcess enables -WhatIf [OK]
Common Mistakes:
  • Assuming Remove-Item lacks -WhatIf support
  • Thinking parameter mandatory affects -WhatIf
  • Believing return statement is required for -WhatIf

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes