I have a project folder called topfolder and I want to find all files in all subfolders that match a certain pattern, e.g. when the folder contains foo anywhere in its name, I want to list all files inside it.
I tried something like:
gci .\topfolder -rec -filter *foo*
but it has two problems:
- Only files actually containing
foowill be listed (I want any file in foo-like folder). - Directories matching this filter will be part of the result set too.
Then I tried this:
gci .\topfolder\*foo* -include *.*
but that doesn't work at all - apparently wildcards match only a single segment of a path so that pattern will match topfolder\foobar but not topfolder\subfolder\foobar.
The only way I've found so far was to use Get-ChildItem twice, like this:
gci .\topfolder -rec -include *foo* | where { $_.psiscontainer } | gci
This works but it seems like a waste to call gci twice and I hope I just didn't find a right wildcard expression for something like -Path, -Filter or -Include.
What is the best way to do that?