如何使用 PowerShell 的 -filter 参数排除值/文件? [英] How can I use PowerShell's -filter parameter to exclude values/files?
问题描述
我目前正在运行 PowerShell (v3.0) 脚本,其中一个步骤是检索目录中的所有 HTML 文件.效果很好:
I'm currently running a PowerShell (v3.0) script, one step of which is to retrieve all the HTML files in a directory. That works great:
$srcfiles = Get-ChildItem $srcPath -filter "*.htm*"
但是,现在我必须识别所有非 HTML 文件...CSS、Word 和 Excel 文档、图片等.
However, now I'm faced with having to identify all the non-HTML files...CSS, Word and Excel docs, pictures, etc.
我想要一些像 -ne
参数结合 -filter
参数那样工作的东西.本质上,给我所有不是 "*.htm*"
I want something that would work like the -ne
parameter in conjunction with the -filter
parameter. In essence, give me everything that's not "*.htm*"
-filter -ne
不起作用,我一时兴起尝试了 -!filter
,但似乎在 MSDN 上的 powershell 文档中找不到任何内容否定 -filter
参数.也许我需要管道一些东西...?
-filter -ne
doesn't work, I tried -!filter
on a whim, and I can't seem to find anything in powershell doc on MSDN to negate the -filter
parameter. Perhaps I need to pipe something...?
有没有人有解决方案?
推荐答案
-Filter
不是正确的方法.改用 -exclude
参数:
-Filter
is not the right way. Use the -exclude
parameter instead:
$srcfiles = Get-ChildItem $srcPath -exclude *.htm*
-exclude
接受 string[]
类型作为输入.通过这种方式,您可以排除多个扩展名/文件类型,如下所示:
-exclude
accepts a string[]
type as an input. In that way you can exclude more than one extension/file type as follows:
$srcfiles = Get-ChildItem $srcPath -exclude *.htm*,*.css,*.doc*,*.xls*
..等等.
这篇关于如何使用 PowerShell 的 -filter 参数排除值/文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!