如何使用 PowerShell 强制删除打开的文件 [英] How to force delete an open file using PowerShell

查看:718
本文介绍了如何使用 PowerShell 强制删除打开的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Remove-Item 命令不会删除正在使用的文件.有什么方法可以删除所有文件而不管它们的状态?

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?

推荐答案

您可以通过找到正在使用该文件的进程然后停止这些进程来完成此操作.然后您可以删除该文件.

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

这篇关于如何使用 PowerShell 强制删除打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆