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.
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.
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.
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.
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.
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.*' }
You may join the filter results in a pipe like this:
@("MyProject.Data*.dll", "EntityFramework*.dll") | %{ Get-ChildItem -File $myPath -Filter $_ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With