Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell Get-ChildItem given multiple -Filters

Tags:

powershell

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.

like image 615
Pricey Avatar asked May 10 '15 11:05

Pricey


People also ask

What is the output of Get-ChildItem?

The Get-ChildItem cmdlet uses the Path parameter to specify the directory C:\Test . Get-ChildItem displays the files and directories in the PowerShell console. By default Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item.

How do I Get-ChildItem to exclude folders?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.

How do I get a list of files in a directory and subfolders using PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.

What is difference between Get item and Get-ChildItem?

Differences Between Get-Item and Get-ChildItemGet-Item returns information on just the object specified, whereas Get-ChildItem lists all the objects in the container. Take for example the C:\ drive.


2 Answers

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.*' } 
like image 111
Frode F. Avatar answered Sep 18 '22 14:09

Frode F.


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

@("MyProject.Data*.dll", "EntityFramework*.dll") | %{ Get-ChildItem -File $myPath -Filter $_ } 
like image 35
Hartmut Jürgens Avatar answered Sep 18 '22 14:09

Hartmut Jürgens