While I have no explanation for your symptom, you can bypass it by streamlining your code and avoiding Set-Location calls (which are best avoided, because they change the current location session-wide):
Remove-Item (Join-Path $Path *.pdf) -Exclude *sig* -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
The above removes all .pdf files in folder $Path that do not have substring sig in their name - which is what I understand your intent to be.
Wrapped in a function (error handling omitted):
function Remove-AllUnsigned {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='None')]
param (
[Parameter(Mandatory)]
[string] $Path,
[switch] $Force
)
# Ask for confirmation, unless -Force was passed.
# Caveat: The default prompt response is YES, unfortunately.
if (-not $Force -and -not $PSCmdlet.ShouldContinue($Path, "Remove all unsigned PDF files from the following path?")) { return }
# Thanks to SupportsShouldProcess, passing -WhatIf to the function
# is in effect propagated to cmdlets called inside the function.
Remove-Item (Join-Path $Path *.pdf) -Exclude *sig*
}
Note:
Since the function isn't designed to accept pipeline input, there is no need for a process block (though it wouldn't hurt).
Since instant deletion can be dangerous, $PSCmdlet.ShouldContinue() is used to prompt the user for confirmation by default - unless you explicitly pass -Force
To make the function itself also support the -WhatIf common parameter for previewing the operation, property SupportsShouldProcess in the [CmdletBinding()] attribute is set (implicitly to $true), but the ConfirmImpact property is set to None, given that .ShouldContinue() will handle the prompting, unconditionally (note that explicitly using -Confirm would still cause a ShouldProcess-related prompt).
-Nameparameter onGet-ChildItem?PushandPoplocation instead ofSet-Locationor even better, forget about changing directory and work using absolute paths.-Nameis unrelated to the matching behavior of the cmdlet. It merely requests that path strings (relative to the input path) rather than objects be output.