How to avoid Remove-Item PowerShell errors "process cannot access the file"?

Posted by Michael Freidgeim on Server Fault See other posts from Server Fault or by Michael Freidgeim
Published on 2012-02-10T23:04:14Z Indexed on 2012/09/09 9:40 UTC
Read the original article Hit count: 160

Filed under:
|
|

We are using TfsDeployer and PowerShell script to remove the folders ising Remove-Item before deployment of a new version. Sometimes the PS script failed with the error

Remove-Item : Cannot remove item Services\bin: The process cannot access the file Services\bin' because it is being used by another proc Get-ChildItem -Path $Destination -Recurse | Remove-Item <<<< -force -recurse + CategoryInfo : WriteError: (C:\Program File..\Services\bin:DirectoryInfo) [Remove-Item], IOException FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

I’ve tried to follow the answer from PowerShell remove force to pipe get-childitem -recurse into remove-item.

get-childitem * -include *.csv -recurse | remove-item

,but the error still happens periodically. We are using unlocker to manually kill locking application, (it’s usually w3wp), but I prefer to find automated solution.

Another (not ideal) option is to-suppress-powershell-errors get-childitem -recurse -force -erroraction silentlycontinue

Any suggestions are welcome.

© Server Fault or respective owner

Related posts about powershell

Related posts about deployment