Reputation: 38437
I have two string arrays in my PowerShell script:
$search = @('File1', 'File2');
$paths = @('C:\Foo\File1.pdf', 'C:\Foo\Bar.doc', 'C:\Foo\File2.txt');
How can I get all file paths which contain the file names from the search array? Can this be done in a pipeline?
Upvotes: 1
Views: 186
Reputation: 200213
If you need to do partial matches you could do something like this:
$paths | Where-Object {
$filename = Split-Path $_ -Leaf
$search | Where-Object { $filename -like "*$_*" }
}
Upvotes: 1
Reputation: 58931
You could use the GetFileNameWithoutExtension method to retrieve the filename of the path and use -in
to filter them:
$paths | ? { [System.IO.Path]::GetFileNameWithoutExtension($_) -in $search }
Upvotes: 3