How to force delete an open file using PowerShell

﹥>﹥吖頭↗ 提交于 2021-01-27 07:05:28

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/45713467/how-to-force-delete-an-open-file-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!