如何使用PowerShell 2.0递归删除整个目录? [英] How to recursively delete an entire directory with PowerShell 2.0?

查看:1279
本文介绍了如何使用PowerShell 2.0递归删除整个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PowerShell中强制删除目录及其所有子目录的最简单方法是什么?我在Windows 7中使用了PowerShell V2。



我从几个来源得知,最明显的命令是 Remove-Item $ targetDir -Recurse - 强制,无法正常工作。这包括PowerShell V2联机帮助(使用 Get-Help Remove-Item -Examples 找到)中的一条语句:


...因为此cmdlet中的Recurse参数有错误,所以该命令使用Get-Childitem cmdlet获取所需的文件,并使用管道运算符将它们传递给Remove- Item cmdlet ...

我已经看到了各种使用 Get-ChildItem 的例子, strong> Remove-Item ,但这些示例通常会删除基于过滤器的一些文件,而不是整个目录。



我正在寻找最干净的方式吹出一个完整的目录,文件和子目录,而不用用最少量的代码生成任何用户警告消息。如果这很容易理解,那么一行就好。

解决方案

  Remove- Item -Recurse -Force some_dir 

确实可以在这里做广告。



据我了解,当您尝试递归删除一组已过滤的文件时, -Recurse 参数就无法正常工作。杀死一个单独的目录和下面的一切似乎工作正常。


What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

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:

...Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet...

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.

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递归删除整个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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