Get-Childitem filter files with multiple extensions?

A

akcorr

Is there any way to run a get-childitem and filter for multiple objects?
Example
Get-Childitem -path C:\ -filter *.log,*.txt

I guess I could enter the extensions in an array and run though a loop
appending the results? Just wondering if there was an easier way
 

My Computer

K

Kiron

Use the -Include parameter to qualify the -Path which should have a '\*'

ls c:\* -i *.log, *.txt

--
Kiron
 

My Computer

B

Brandon Shell [MVP]

Try this. Include takes an array and filter only uses one string.

dir -path c:\data -Include @("*.txt","*.log") -rec

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

a> Is there any way to run a get-childitem and filter for multiple
a> objects?
a> Example
a> Get-Childitem -path C:\ -filter *.log,*.txt
a> I guess I could enter the extensions in an array and run though a
a> loop appending the results? Just wondering if there was an easier
a> way
a>
 

My Computer

A

akcorr

Nice. Thanks. I was on right track...I did -include "*.txt,*.log" instead
but it obviously failed. Thanks for your help!

"Brandon Shell [MVP]" wrote:

> Try this. Include takes an array and filter only uses one string.
>
> dir -path c:\data -Include @("*.txt","*.log") -rec
>
> Brandon Shell
> ---------------
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
>
> a> Is there any way to run a get-childitem and filter for multiple
> a> objects?
> a> Example
> a> Get-Childitem -path C:\ -filter *.log,*.txt
> a> I guess I could enter the extensions in an array and run though a
> a> loop appending the results? Just wondering if there was an easier
> a> way
> a>
>
>
>
 

My Computer

S

Shay Levy [MVP]

Hi akcorr,

You can with -include, just make sure to add the '*' wildcard to the end
of the path (it leads to the contents of the directory).
You can omit the wildcard if you specify -recurse:

PS > Get-Childitem -path C:\* -include *.log,*.txt


---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic



a> Is there any way to run a get-childitem and filter for multiple
a> objects?
a> Example
a> Get-Childitem -path C:\ -filter *.log,*.txt
a> I guess I could enter the extensions in an array and run though a
a> loop appending the results? Just wondering if there was an easier
a> way
a>
 

My Computer

Top