Powershell 5 Get-ChildItem LiteralPath no longer works with Include

I have now upgraded to Windows 10 TH2 Build 10586 using PowerShell 5.0.10586.0

Now I am having a problem with Get-ChildItem

$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Include *.txt 

This returns ALL files in $ path, even if they are not .txt. This worked before the update. When I change it to

 $files = Get-ChildItem -Path $path -Force -Recurse -Include *.txt 

he is working again. But that is not what I want. Is this a mistake or am I doing something wrong?

+7
powershell
source share
3 answers

Personally, I no longer use -Include or -Exclude . I always go through the Where-Object . I don’t know if the author of -Include and -Exclude crazy or there were problems with the underlying .Net provider, but they were hacked like 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 should be the right behavior? Does the -Recurse flag -Recurse way -Include works? I dont know. I do not care. I am not going to engage in such behavior. I just use this:

 gci -Path $path -Recurse -Force | Where-Object { $_.Extension -eq '.txt' } 

I rely on Get-ChildItem to list files and folders and what it is. Just give me the objects and I will filter them out. Like all old Remove-Item -Recurse errors, there is something out there that just doesn't work the way people expect it to.

+4
source share
+1
source share

Note that -Filter does not seem to have this problem. It works:

 $files = Get-ChildItem -LiteralPath $path -Force -Recurse -Filter *.txt 

Filter also more efficient because it is used by the underlying provider (unlike Include , which is used by PowerShell itself, where added by your code).

However, Filter accepts only one template parameter, while Include supports several templates .

0
source share

All Articles