Searching files by multiple search patterns in PowerShell
Andrew Mclaughlin
I got this piped command:
Get-Childitem -Path E:\Shares\ -Recurse -Filter *Synformulas* | Export-csv -Path E:\result.csvNow it is like that, that I need to search for multiple search terms. I cannot append simply them to the -Filter flag.
Can anybody tell me how can search through a folder recursively for file names with multiple search terms?
2 Answers
The -Filter parameter only accepts a single string. However, the -Include parameter accepts multiple values, but qualifies the -Path argument.
The idea is to append \* to the end of the path, and then use-Include to select multiple wildcard strings.
Note that quoting strings is unnecessary in arguments unless they
contain spaces or special characters.
Example:
Get-Childitem -Path E:\Shares\* -Recurse -Filter *Synformulas*, *otherformula* | Export-csv -Path E:\result.csv One way is probably just to filter the files as a step in your pipeline:
Get-Childitem -Path E:\Shares\ -Recurse | Where-Object { $_.Name -Like "*Foo*" -or $_.Name -Like "*Bar*" } | Export-csv -Path E:\result.csvThis also lets you use -Match for regex filtering if you prefer:
... | Where-Object {$_.Name -match 'Foo|Bar'} | ...