Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force delete an open file using PowerShell

Remove-Item command does not delete files which are in use. Is there any way where we can delete all the files irrespective of their state?

like image 861
abhinay kumar Avatar asked Nov 07 '22 18:11

abhinay kumar


1 Answers

You can do this is by finding the processes that are using the file then stop the processess.You can then delete the file after.

$allProcesses = Get-Process
#$lockedFile is the file path
foreach ($process in $allProcesses) { 
$process.Modules | where {$_.FileName -eq $lockedFile} | Stop-Process
-Force -ErrorAction SilentlyContinue
    }
Remove-Item $lockedFile
like image 91
Code Demon Avatar answered Nov 15 '22 06:11

Code Demon