In my PowerShell script I'm trying to delete a folder, but only if it exists:
if (Test-Path $folder) { Remove-Item $folder -Recurse; }
I find myself repeating this combination of cmdlets quite a few times, and wished I had something like this:
# Pseudo code:
Remove-Item $folder -Recurse -IgnoreNonExistentPaths
Is there a way to do something like that? A different command, or an option I've missed from the Remove-Item
documentation perhaps? Or is the only way to DRY out this bit of code to write my own cmdlet that combines Test-Path
and Remove-Item
?