30

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?

1
35
dir .\* -include ('*.xsl', '*.xslt') -recurse
3
  • Note that the () is actually redundant, but I prefer to be explicit. – EBGreen Aug 2 '11 at 18:25
  • 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 Jun 22 '17 at 14:50
  • 1
    the answer by crysman is faster superuser.com/a/1056648/440382 – crysman Mar 3 '20 at 8:53
17

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

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

The "filters" *.xsl and *.xslt can be part of the path parameter as well. Path can take a string array:

Set-Location c:\topLevel
gci *.xsl, *.xslt -Recurse
0

This is older, but it helped me write this script to update URL's in desktop and/or favorite shortcuts, so I thought I would share:

foreach ($link in GCI "$env:USERPROFILE\Desktop" | Where-Object {$_.extension -in '.lnk','.url'}) {
   $shell = New-Object -COM WScript.Shell
   $shortcut = $shell.CreateShortcut($link.FullName)
   $shortcut.TargetPath = $shortcut.TargetPath -replace "old-url.com","new-url.com"
   $shortcut.Save()
}

foreach ($link in GCI "$env:USERPROFILE\Favorites" | Where-Object {$_.extension -in '.lnk','.url'}) {
   $shell = New-Object -COM WScript.Shell
   $shortcut = $shell.CreateShortcut($link.FullName)
   $shortcut.TargetPath = $shortcut.TargetPath -replace "old-url.com","new-url.com"
   $shortcut.Save()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy