Steps to delete file that locked by Windows Process
- Open Task Manager – Performance – Open Resource Manager
- Go to CPU – Associated Handles and search for the file which locked by Windows Process
Right click on the process which locked the file to end / kill it, and you should be able to delete the file in Windows Explore now
The PowerShell way
-
Download Get-FileLockProcess.ps1
-
Import the downloaded Function, End the process and delete the file
#Import Function
. C:\MyPowerShell\Get-FileLockProcess.ps1
$File = "C:\Temp\SendEmail.csv"
$Process = Get-FileLockProcess -FilePath $File
#Kill the process
Stop-Process -Id $Process.id
#Wait for 5 seconds
Start-Sleep 5
#Delete the file
Remove-Item $File -Force