问题描述
在 PowerShell 中强制删除目录及其所有子目录的最简单方法是什么?我在 Windows 7 中使用 PowerShell V2.
What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.
我从几个来源了解到,最明显的命令 Remove-Item $targetDir -Recurse -Force
无法正常工作.这包括 PowerShell V2 在线帮助(使用 Get-Help Remove-Item -Examples
找到)中的声明:
I have learned from several sources that the most obvious command, Remove-Item $targetDir -Recurse -Force
, does not work correctly. This includes a statement in the PowerShell V2 online help (found using Get-Help Remove-Item -Examples
) that states:
...由于此cmdlet中的Recurse参数有问题,该命令使用Get-Childitem cmdlet获取所需文件,并使用管道运算符将它们传递给Remove-Item cmdlet...
我见过各种使用 Get-ChildItem 并将其通过管道传递给 Remove-Item 的示例,但这些示例通常会根据过滤器删除一组文件,而不是整个目录.
I have seen various examples that use Get-ChildItem and pipe it to Remove-Item, but the examples usually remove some set of files based on a filter, not the entire directory.
我正在寻找最干净的方法来清除整个目录、文件和子目录,而不会使用最少的代码生成任何用户警告消息.如果它易于理解,单行将很好.
I am looking for the cleanest way to blow out an entire directory, files and child directories, without generating any user warning messages using the least amount of code. A one-liner would be nice if it is easy to understand.
推荐答案
Remove-Item -Recurse -Force some_dir
确实像这里宣传的那样工作.
does indeed work as advertised here.
rm -r -fo some_dir
是也可以使用的速记别名.
are shorthand aliases that work too.
据我了解,当您尝试递归删除一组过滤的文件时,-Recurse
参数无法正常工作.杀死一个目录和它下面的所有东西似乎都可以正常工作.
As far as I understood it, the -Recurse
parameter just doesn't work correctly when you try deleting a filtered set of files recursively. For killing a single dir and everything below it seems to work fine.
这篇关于如何使用 PowerShell 2.0 递归删除整个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!