PowerShell过滤多个参数

时间:2014-02-27 15:57:59

标签: powershell

我想知道是否可以将多个搜索过滤器指定为一次。例如,我有这行代码可以找到所有包含“&”的文件。符号。

get-childitem ./ -Recurse -Filter "*&*" | 
    ? { $_.PSIsContainer } | 
    Select-Object -Property FullName

我想扩展这个以便我可以搜索一次并找到包含%,$,@等其他符号的文件。我想找到包含这些符号的文件,而不是所有符号的文件他们所以我认为在某个地方需要一个OR。我尝试了以下代码,但它似乎对我不起作用:

get-childitem ./ -Recurse -Filter "*&*" -Filter "%" | 
    ? { $_.PSIsContainer } | 
    Select-Object -Property FullName

2 个答案:

答案 0 :(得分:2)

您可以使用-match运算符和正则表达式:

Get-ChildItem -Recurse | 
    Where { !$_.PSIsContainer -and ($_.name -match '&|%|\$|@')} | 
    Select-Object -Property FullName

如果您使用的是PowerShell v3或更高版本,可以稍微简化一下:

Get-ChildItem -Recurse -File | 
    Where Name -match '&|%|\$|@' | 
    Select-Object -Property FullName

答案 1 :(得分:2)

如果你有V3或更好,你可以利用“通配”通配符功能:

get-childitem './*[&%$@]*' -Recurse | where {$_.PSIsContainer} 

如果你有V4,你可以省去$ _. PSIsContainer过滤器并使用-Directory开关:

get-childitem './*[&%$@]*' -Recurse -Directory