Bird
0
0

How can you modify this advanced function to confirm before making changes?

hard📝 Application Q9 of 15
PowerShell - Functions
How can you modify this advanced function to confirm before making changes?
function Remove-ItemSafe {
  [CmdletBinding()]
  param(
    [string]$Path
  )
  Remove-Item -Path $Path
}
AAdd <code>ValueFromPipeline=$true</code> to the Path parameter only
BAdd <code>Mandatory=$true</code> to the Path parameter
CAdd <code>SupportsShouldProcess=$true</code> to CmdletBinding and use <code>if ($PSCmdlet.ShouldProcess($Path))</code> before Remove-Item
DUse <code>Write-Verbose</code> to confirm before Remove-Item
Step-by-Step Solution
Solution:
  1. Step 1: Enable ShouldProcess support

    Add SupportsShouldProcess=$true in CmdletBinding to enable -WhatIf and -Confirm support.
  2. Step 2: Use ShouldProcess method

    Wrap Remove-Item call inside if ($PSCmdlet.ShouldProcess($Path)) to confirm action before execution.
  3. Step 3: Evaluate other options

    Add Mandatory=$true to the Path parameter adds mandatory but no confirmation. Add ValueFromPipeline=$true to the Path parameter only adds pipeline input but no confirmation. Use Write-Verbose to confirm before Remove-Item only writes verbose message, no confirmation.
  4. Final Answer:

    Add SupportsShouldProcess=$true and use ShouldProcess check -> Option C
  5. Quick Check:

    SupportsShouldProcess + ShouldProcess method = confirmation support [OK]
Quick Trick: Use SupportsShouldProcess and ShouldProcess for confirmations [OK]
Common Mistakes:
  • Forgetting to add SupportsShouldProcess
  • Not wrapping action in ShouldProcess check
  • Confusing verbose messages with confirmation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes