Powershell 5 Get-ChildItem LiteralPath不再适用于Include

时间:2015-11-13 00:52:25

标签: powershell powershell-v5.0

现在我使用PowerShell 5.0.10586.0更新到Windows 10 TH2 Build 10586

现在我遇到了Get-ChildItem的问题

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

这将返回$ path中的所有文件,即使它们不是.txt。 这在更新之前有效。 当我将其更改为

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

它再次起作用。但这不是我想要的。 这是一个错误还是我做错了什么?

3 个答案:

答案 0 :(得分:5)

就个人而言,我永远不会再次使用-Include-Exclude。我总是通过Where-Object。我不知道-Include-Exclude的作者是否疯了,或者是否存在底层.Net提供商的问题,但他们会像地狱一样狡猾。 / p>

我在5.0.10240.16384。

gci -Path $path -Include *.txt -Force

什么都不返回。

gci -LiteralPath $path -Include *.txt -Force

返回$path中的所有内容。

gci -LiteralPath $path -Include *.txt -Force -Recurse
gci -Path $path -Include *.txt -Force -Recurse

两者都返回$path和所有子文件夹中的* .txt。

那么应该采取的正确行为是什么? -Recurse标志是否会修改-Include的工作方式?我不知道。我不再关心了。我不打算处理那种行为。我只是用这个:

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

我依靠Get-ChildItem枚举文件和文件夹及其。只要给我这些物品,我就会过滤它们。像所有旧的Remove-Item -Recurse错误一样,那里的某些东西并没有像人们期望的那样工作。

答案 1 :(得分:1)

答案 2 :(得分:0)

请注意,-Filter似乎没有此问题。这有效:

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

Filter也是more efficient,因为它由底层提供程序使用(而不是PowerShell本身应用的Include,就像添加了where子句一样通过你的代码)。

然而Filter only accepts one pattern parameter, whereas Include supports multiple patterns