Pricey
Pricey

Reputation: 5929

powershell Get-ChildItem given multiple -Filters

Is there a syntax for the -Filter property of Get-ChildItem to allow you to apply multiple filters at the same time? i.e. something like the below where I want to find a few different but specific named .dll's with the use of a wildcard in both?

Get-ChildItem -Path $myPath -Filter "MyProject.Data*.dll", "EntityFramework*.dll"

or do I need to split this into multiple Get-ChildItem calls? because I'm looking to pipe the result of this.

Upvotes: 48

Views: 73774

Answers (4)

Thierry Wiersma
Thierry Wiersma

Reputation: 1

You could use both -Filter and -Include at the same time

Get-ChildItem -Path $myPath -Filter "*.dll" -Include "MyProject.Data*.dll", "EntityFramework*.dll"

Upvotes: 0

Hartmut Jürgens
Hartmut Jürgens

Reputation: 131

You may join the filter results in a pipe like this:

@("MyProject.Data*.dll", "EntityFramework*.dll") | %{ Get-ChildItem -File $myPath -Filter $_ }

Upvotes: 13

Frode F.
Frode F.

Reputation: 54871

The -Filter parameter in Get-ChildItem only supports a single string/condition AFAIK. Here's two ways to solve your problem:

You can use the -Include parameter which accepts multiple strings to match. This is slower than -Filter because it does the searching in the cmdlet, while -Filter is done on a provide-level (before the cmdlet gets the results so it can process them). However, it is easy to write and works.

#You have to specify a path to make -Include available, use .\* 
Get-ChildItem .\* -Include "MyProject.Data*.dll", "EntityFramework*.dll"

You could also use -Filter to get all DLLs and then filter out the ones you want in a where-statement.

Get-ChildItem -Filter "*.dll" .\* | Where-Object { $_.Name -match '^MyProject.Data.*|^EntityFramework.*' }

Upvotes: 64

Andrew Semenov
Andrew Semenov

Reputation: 151

You can only use one value with -Filter, whereas -Include can accept multiple values, for example ".dll, *.exe".

Upvotes: 3

Related Questions