How do I get get-childitem to filter on multiple file types?

22

2

I'm trying to get a list of all the XSL and XSLT files in a directory.

dir -recurse -filter *.xsl,*.xslt -name

But the following error:

Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.

dir -recurse -filter *.xsl -filter *.xslt -name

But got this error:

Get-ChildItem : Cannot bind parameter because parameter 'Filter' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3".

Can I list both file extensions with a single command?

Amy

Posted 2011-08-02T17:43:20.903

Reputation: 942

Answers

33

dir .\* -include ('*.xsl', '*.xslt') -recurse

EBGreen

Posted 2011-08-02T17:43:20.903

Reputation: 7 834

It makes absolutely no sense why this command does not work without the -recurse switch, to list .xsl and .xslt files in the current directory. – Gordon Bell – 2017-06-22T14:50:28.163

Note that the () is actually redundant, but I prefer to be explicit. – EBGreen – 2011-08-02T18:25:57.067

7

I do not know why, but this one seems to be much faster:

dir .\ -recurse | where {$_.extension -in ".xsl",".xslt"}

crysman

Posted 2011-08-02T17:43:20.903

Reputation: 393