Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get-Childitem filter files with multiple extensions?

4,552 views
Skip to first unread message

akcorr

unread,
Jul 16, 2008, 12:35:01 PM7/16/08
to
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

Kiron

unread,
Jul 16, 2008, 12:44:45 PM7/16/08
to
Use the -Include parameter to qualify the -Path which should have a '\*'
 
ls c:\* -i *.log, *.txt

--
Kiron

Brandon Shell [MVP]

unread,
Jul 16, 2008, 12:49:03 PM7/16/08
to
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>


Shay Levy [MVP]

unread,
Jul 16, 2008, 1:48:50 PM7/16/08
to
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>


Marco Shaw [MVP]

unread,
Jul 16, 2008, 12:51:50 PM7/16/08
to

If you check 'get-help get-childitem -full', you'll see that this
parameter only accepts a string. It also accepts a wildcard, which I
don't think will help.

You can also try this (less efficient):

get-childitem-path c:\ -recurse -include *.ps1,*.txt

(I'm using v2 CTP2, so if your results may be different if you're using v1.)

Marco

--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

akcorr

unread,
Jul 16, 2008, 1:29:03 PM7/16/08
to
Nice. Thanks. I was on right track...I did -include "*.txt,*.log" instead
but it obviously failed. Thanks for your help!

Kiron

unread,
Jul 16, 2008, 1:55:00 PM7/16/08
to
Thanks Keith.

--
Kiron

0 new messages