1

Recently I was to create a report via PowerShell about the *.inf files located in the C:\Windows\winsxs folder unsing the Filter parameter of Get-ChildItem (see the official documentation about this cmdlet and its parameters), like:

$infFiles = Get-ChildItem -Path C:\Windows\winsxs -Filter *.inf -Recurse

I've noticed it however, that files having other file extensions, like .inf_loc or .inf_dbf42768 are included in the result as well.

On the other hand, the Include parameter works as expected, returning only the .inf files:

$infFiles = Get-ChildItem -Path C:\Windows\winsxs -Include *.inf -Recurse

I've tested the phenomenon using PS version 3 and 4, and found, that if the extension you are looking for has exactly 3 character, than any files, those extensions begin with the same 3 character are returned, even if those files have a longer extension. If the extension you are looking for is shorter or longer than 3 characters, there is no such issue, at least, based on my experience.

Although performance is in my case not crucial, as far as I understand, using the Filter parameter would be more efficient, than the Include parameter, as it performs the filtering already on the provider level, as discussed in the cmdlet description:

Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

and here:

...the –Filter parameter generates early, early filtering, whereas-Include is later early filtering! The performance difference between the two approaches turns out to be significant!

Is it a documented behaviour / bug? I've found something similar in the post mentioned before as well, but not in the official documentations.

pholpar
  • 1,725
  • 2
  • 14
  • 23

1 Answers1

1

Per the linked question, this seems like a limitation of the -Filter parameter. I guess another solution would be to still use -Filter for its performance benefit but then filter out the extra results you don't want afterwards with Where-Object:

Get-ChildItem -filter "*.inf" | where {$_ -match "\.inf$"}

This uses the regex $ character to return only where .inf is at the end of the string.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68