I am running the following in my PowerShell script:
Get-ChildItem -LiteralPath $source -Force -Recurse -Directory | ForEach-Object {
Write-Output "Removing files in folder $($_.FullName)"
Get-ChildItem -LiteralPath $_.FullName -Force -File | ForEach-Object {
Write-Output "Removing file $($_.FullName)"
Remove-Item -LiteralPath $_.FullName -Force -ErrorAction Stop
}
}
This is a PowerShell script running inside an Azure Automation Account, to be able to cleanup folders on a Panzura File Server when they are no longer needed.
This is failing and giving me logs like:
Removing files in folder C:\Folder\SubFolder
Removing files in folder C:\Folder\SubFolder\SubSubFolder
Removing files in folder C:\Folder\SubFolder\SubSubSubFolder
Removing file C:\Folder\SubFolder\SubSubSubFolder\SomeFile.tmp
Could not find file 'C:\Folder\SubFolder\SubSubSubFolder\SomeFile.tmp'
Even though the file was found inside a sub-folder when using the folder's LiteralPath in Get-ChildItem and then FullName when using Remove-Item.
It's like these files are locked somehow and cannot be deleted and so these errors are thrown.
How can I avoid this? Is there any way to forcefully delete these "locked" files, more than using '-Force' in my command?
Thanks!