如何使用powershell删除具有特定名称的所有文件夹
我有一个文件夹,我必须从中删除所有名为“tempspecsuite”的子文件夹 我的文件夹结构如下所示如何使用powershell删除具有特定名称的所有文件夹,powershell,Powershell,我有一个文件夹,我必须从中删除所有名为“tempspecsuite”的子文件夹 我的文件夹结构如下所示 折叠1 src 目标 tempspecsuite_0 tempspecsuite_1 我希望递归删除名为tempspecsuite的文件夹_ 我试着使用下面的命令,但不起作用 Get-Childitem -path C:\folder1 -Recurse | where-object {$_.Name -ilike "*tempspecssuite_*"} | Rem
- src
- 目标
- tempspecsuite_0
- tempspecsuite_1
Get-Childitem -path C:\folder1 -Recurse | where-object {$_.Name -ilike "*tempspecssuite_*"} | Remove-Item -Force -WhatIf
管道对象需要使用
$\uuuu
引用,而不是$
,如下所示:
Get-Childitem -Path C:\folder1 -Recurse | Where-Object {$_.Name -ilike "*tempspecssuite*"} | Remove-Item -Force -WhatIf
也可以使用$PSItem
引用,或使用公共参数设置为自定义变量。您可以在和中找到更多信息
您还可以通过使用中的-Filter
参数来简化上述操作,该参数也接受通配符:
Get-ChildItem -Path C:\folder1 -Directory -Filter tempspecssuite_* -Recurse | Remove-Item -Force -Recurse -WhatIf
这允许Get ChildItem
在检索文件时过滤文件,而不是在检索后使用Where Object
进行过滤
您还可以使用
-Directory
开关限制筛选,因为我们只关心删除目录。管道对象需要使用$\uuuu
,而不是$
,如下所示:
Get-Childitem -Path C:\folder1 -Recurse | Where-Object {$_.Name -ilike "*tempspecssuite*"} | Remove-Item -Force -WhatIf
也可以使用$PSItem
引用,或使用公共参数设置为自定义变量。您可以在和中找到更多信息
您还可以通过使用中的-Filter
参数来简化上述操作,该参数也接受通配符:
Get-ChildItem -Path C:\folder1 -Directory -Filter tempspecssuite_* -Recurse | Remove-Item -Force -Recurse -WhatIf
这允许Get ChildItem
在检索文件时过滤文件,而不是在检索后使用Where Object
进行过滤
您还可以使用
-Directory
开关限制筛选,因为我们只关心删除目录。您缺少下划线$。Name
应该是$。.Name
谢谢。。我的问题中有一个输入错误,但问题是我没有删除您缺少的下划线$。Name
应该是$\uname
谢谢Itchydon。。我的问题中有一个输入错误,但问题是我没有删除WhatifThank road runner!它现在正在识别目录,但没有删除文件夹。有什么是我吗missing@DevX您是否仍在使用-WhatIf
?这只会告诉您命令的效果,而不会实际删除。删除此参数以执行真正的删除。很抱歉,是的,它正在工作!!谢谢汉克斯路行者!它现在正在识别目录,但没有删除文件夹。有什么是我吗missing@DevX您是否仍在使用-WhatIf
?这只会告诉您命令的效果,而不会实际删除。删除此参数以执行真正的删除。很抱歉,是的,它正在工作!!谢谢