Filter Get-ChildItem Using Array

I just started using PowerShell today, and I have intent files with several templates in an array, for example:

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

I want to list files in $sourceDirwhere matches any element from the specified array.

I can do this and it works:

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

Just for training, can I do this in pipe lining?

Something like this:

Get-ChildItem $sourceDir | Where-Object { $matchPattern -contains $_.Name  }
+5
source share
2 answers

You can simply do:

gci "$someDir\*" -include $matchPattern
+7
source

Something like this should work.

Assuming the array $ a exists with some lines in it:

gci $someDir | %{$a -eq $_.name}

, , gci, $a, . , $someDir = C:\ "windows" $a, "", .

: , , * , , . .

.

[regex]$patt = "^(Win.*|.*Files)$"

, .

gci $someDir | ?{$_.name -match $patt}
0

All Articles