I am using this command to remove multiple files in two different directories:
Remove-Item -path c:\tmp\folder1\*, c:\tmp\folder1\* -Force -Recurse
both folders contain some zip files and sub folders which i want te remove
I need to check if those folders (folder1 and folder2) exist and not empty before executing this command. Can't figure it out :(
Any help would be appreciated.
Get-ChildItem(learn.microsoft.com/en-us/powershell/module/…).Test-Pathto see if the dir exists. do you really care if there is anything in the target dir?Test-Pathfor some reason I can't think of, you could simply ignore errors on folders that do not exist by writingRemove-Item -path c:\tmp\folder1\*, c:\tmp\DoesNotExist\* -Force -Recurse -ErrorAction SilentlyContinue. Of course this will also hide errors when there are files in there you may not delete because of lack of permissions..