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?
To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter. Gets files.
Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.
To get the full path of the file in PowerShell, use the Get-ChildItem to get files in the directory and pass the output to foreach-object to iterate over the file and get the full name of the file.
Get-ChildItem (GCI) gets items and if the item is a container, it will get child items available inside the container. Location specified in PowerShell Get-ChildItem can be file system directory, registry, or certificate store. Let's understand the PowerShell Get-ChildItem cmdlet with examples.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With