Right now I updated to Windows 10 TH2 Build 10586 with PowerShell 5.0.10586.0
Now I got a problem with Get-ChildItem
$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Include *.txt
This returns ALL files in $path even they are not .txt.
This was working before the update.
When I change it to
$files = Get-ChildItem -Path $path -Force -Recurse -Include *.txt
it works again. But that's not what I want.
Is this a bug or am I doing something wrong?
Personally, I never use -Include
or -Exclude
anymore. I always pipe through Where-Object
. I don't know if the author of -Include
and -Exclude
was insane or if there's a problem with the underlying .Net provider, but they're flaky as hell.
I'm on 5.0.10240.16384.
gci -Path $path -Include *.txt -Force
Returns nothing.
gci -LiteralPath $path -Include *.txt -Force
Returns everything in $path
.
gci -LiteralPath $path -Include *.txt -Force -Recurse
gci -Path $path -Include *.txt -Force -Recurse
Both return *.txt in $path
and all subfolders.
So what's the proper behavior supposed to be? Does the -Recurse
flag modify how -Include
works? I don't know. I no longer care. I'm not going to deal with that kind of behavior. I just use this:
gci -Path $path -Recurse -Force | Where-Object { $_.Extension -eq '.txt' }
I rely on Get-ChildItem
to enumerate files and folders and that's it. Just give me the objects and I'll filter them. Like all the old Remove-Item -Recurse
bugs, there's something there that just doesn't work the way people expect it to.
I think this is a regression. I submitted it as v5 regression: Get-ChildItem -LiteralPath -Recurse ignores -Include and gets all items
Note that -Filter
does not seem to have this issue. This works:
$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Filter *.txt
Filter
is also more efficient, because it is used by the underlying provider (as opposed to Include
which is applied by PowerShell itself, much like a where
clause added by your code).
However Filter
only accepts one pattern parameter, whereas Include
supports multiple patterns.