Bird
0
0

Given this function with Confirm support:

medium📝 Predict Output Q13 of 15
PowerShell - Scripting Best Practices
Given this function with Confirm support:
[CmdletBinding(SupportsShouldProcess=$true)]
function Remove-File {
  param([string]$Path)
  if ($PSCmdlet.ShouldProcess($Path)) {
    Remove-Item $Path
  }
}

What happens when you run Remove-File -Path 'test.txt' -WhatIf?
AIt shows a message about deleting 'test.txt' but does not delete it.
BIt deletes 'test.txt' without asking.
CIt asks for confirmation before deleting 'test.txt'.
DIt throws an error because -WhatIf is not supported.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the function's use of ShouldProcess

    The function calls $PSCmdlet.ShouldProcess($Path), which respects -WhatIf and -Confirm.
  2. Step 2: Effect of running with -WhatIf

    Using -WhatIf causes ShouldProcess to return false but show what would happen, so Remove-Item is not called.
  3. Final Answer:

    It shows a message about deleting 'test.txt' but does not delete it. -> Option A
  4. Quick Check:

    -WhatIf shows action, no delete = B [OK]
Quick Trick: ShouldProcess respects -WhatIf to simulate actions [OK]
Common Mistakes:
  • Assuming -WhatIf deletes files
  • Confusing -WhatIf with -Confirm
  • Thinking function lacks support for -WhatIf

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes