2014-02-27 56 views
1

我想知道是否可以一次指定多個搜索篩選器。例如,我有這行代碼可以找到所有具有「&」符號的文件。PowerShell篩選多個參數

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

我想延長,這樣我可以搜索一個時間,找到我想找到任何這些符號的文件,而不是neccesarly其他符號如%,$,@等文件包含所有這些文件的文件,所以我認爲需要在某處存在OR。我想下面的代碼,但它似乎並沒有爲我工作:

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

回答

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 
+0

我會使用一個字符組('[&%$ @] ')而不是交替。 –

+0

是的,那會更好。謝謝。 –

2

如果你有V3或更好的,你可以利用「通配」通配符功能:

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

如果你有V4,您可以用$ _ PSIsContainer過濾器分配和使用-Directory開關:

get-childitem './*[&%[email protected]]*' -Recurse -Directory