繁体   English   中英

如何在Powershell中将Get-ChildItem与过滤器数组一起使用?

[英]How to use Get-ChildItem with filter array in Powershell?

这个删除某些文档的脚本不起作用:

$includeExtensions = @("*.doc", "*.docx")
get-childitem -Path "C:\somedir" -Include $includeExtensions | remove-item

为什么不? 以及如何指定扩展数组,然后可以将其作为参数传递?

您可以通过管道传递Get-ChildItem的结果以仅对所需的扩展名进行过滤。

$includeExtensions  = @(".doc", ".docx")
Get-ChildItem -Path "C:\somedir" | ?{$includeExtensions -contains $_.Extension} | Remove-Item

尝试像这样指定它:

Get-ChildItem c:\somedir\* -Inc $includeExtensions | remove-item -whatif

从关于Get-ChildItem的文档中:

仅当命令包含Recurse参数或路径指向目录的内容(例如C:\\ Windows *,其中通配符指定C:\\ Windows目录的内容)时,Include参数才有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM