Powershell 5 Get-ChildItem LiteralPath doesn't work with Include anymore

你。 提交于 2019-11-30 19:36:40

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!