Exercise 3: A Correct Remove-OldLog Function — Possible Solution ==================================================================== THE FUNCTION ------------------------------ function Remove-OldLog { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory, ValueFromPipeline)] [string]$Path ) process { if ($PSCmdlet.ShouldProcess($Path, "Delete")) { Remove-Item -Path $Path -ErrorAction Stop } } } HOW IT AVOIDS THE process{} GOTCHA ------------------------------ The Remove-Item call sits inside a process {} block, not directly in the bare function body. Per this chapter's own Show-NameBroken/Show-NameFixed example, this ensures the block re-executes once for every single object piped in, rather than running only once and acting on just the last item received - every path piped into Remove-OldLog gets genuinely processed, not just the final one. HOW IT AVOIDS THE SupportsShouldProcess GOTCHA ------------------------------ [CmdletBinding(SupportsShouldProcess)] is declared on the function itself, and the actual deletion is wrapped in an if ($PSCmdlet.ShouldProcess($Path, "Delete")) check before Remove-Item ever runs. Per this chapter, this automatically wires up both -WhatIf (a safe preview showing what would happen, with nothing actually deleted) and -Confirm (an interactive prompt before each deletion) - without declaring either parameter manually, exactly matching this chapter's own Remove-TempFile example. WHY THIS WORKS AS AN ANSWER ------------------------------ It provides a correct, working function combining Mandatory/ValueFromPipeline, SupportsShouldProcess, and a process {} block, and correctly explains how each piece specifically avoids one of this chapter's two named gotchas (the missing-process{} bug and giving the function real -WhatIf/-Confirm safety).