Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Get-ChildItem by an array

Tags:

powershell

I have just started using PowerShell today, and I have an intention list files by a few patterns in an array, for instance:

$matchPattern = (
                  "SomeCompany.SaaS.Core.Mvc*",
                  "SomeCompany.SaaS.Core.UI.Framework*"
                );

I want to list files in a $sourceDir where any of the item in the above array matches.

I can do this, and it works:

foreach ($item in $matchPattern)
{
    Get-ChildItem $sourceDir | Where-Object {$_.Name -like $item}
}

Just for learning purposes, can I do it in pipe-lining?

Something similar to this:

Get-ChildItem $sourceDir | Where-Object { $matchPattern -contains $_.Name  }
like image 641
Abdul Munim Avatar asked Dec 10 '11 08:12

Abdul Munim


1 Answers

You can just do:

gci "$someDir\*" -include $matchPattern
like image 58
manojlds Avatar answered Oct 31 '22 20:10

manojlds