[英]How to delete all folders with a specific name using powershell
我有一个文件夹,我必须从中删除所有名称如“tempspecssuite_”的子文件夹
我的文件夹结构如下
我想递归删除名称为 tempspecssuite_ 的文件夹
我尝试使用以下命令,但没有成功
Get-Childitem -path C:\folder1 -Recurse | where-object {$_.Name -ilike "*tempspecssuite_*"} | Remove-Item -Force -WhatIf
管道对象需要用$_
引用,而不是$
,如下所示:
Get-Childitem -Path C:\folder1 -Recurse | Where-Object {$_.Name -ilike "*tempspecssuite*"} | Remove-Item -Force -WhatIf
也可以使用$PSItem
引用或使用-PipelineVariable
通用参数设置为自定义变量。 您可以在about_pipelines
、 about_objects
和about_automatic_variables
中找到更多信息。
您还可以通过使用Get-ChildItem
中的-Filter
参数来简化上述操作,该参数也接受通配符:
Get-ChildItem -Path C:\folder1 -Directory -Filter tempspecssuite_* -Recurse | Remove-Item -Force -Recurse -WhatIf
这允许Get-ChildItem
在检索文件时对其进行过滤,而不是在之后使用Where-Object
进行过滤。
您还可以使用-Directory
开关限制过滤,因为我们只关心删除目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.