Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Searching files by multiple search patterns in PowerShell

Writer Andrew Mclaughlin

I got this piped command:

Get-Childitem -Path E:\Shares\ -Recurse -Filter *Synformulas* | Export-csv -Path E:\result.csv

Now 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.csv

This also lets you use -Match for regex filtering if you prefer:

... | Where-Object {$_.Name -match 'Foo|Bar'} | ...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy