Skip to content

Instantly share code, notes, and snippets.

@adbertram
Created June 18, 2024 15:53
Show Gist options
  • Save adbertram/ed2a840ed8b981af18258f0def356dbd to your computer and use it in GitHub Desktop.
Save adbertram/ed2a840ed8b981af18258f0def356dbd to your computer and use it in GitHub Desktop.
function Remove-ItemSafely {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param([string]$path)
if ($PSCmdlet.ShouldProcess($path, "Remove")) {
Remove-Item -Path $path
}
}
PS> Remove-ItemSafely -Path '\oh\god\wrong\file.txt' -WhatIf
What if: Performing the operation "Remove" on target "\oh\god\wrong\file.txt".
PS> Remove-ItemSafely -Path '\oh\god\wrong\file.txt'
Confirm
Are you sure you want to perform this action?
Performing the operation "Remove" on target "\oh\god\wrong\file.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment