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.