powershell 性能 : Get-ChildItem -Include vs. Get-ChildItem | Where-对象

标签 powershell

我尝试了一些选项来迭代我的目录并在以下命令之间获得巨大的性能差异:

慢:

Get-ChildItem -Directory -Force -Recurse -Depth 3 -Include '$tf'

快速:

Get-ChildItem -Directory -Force -Recurse -Depth 3 | Where-Object Name -eq '$tf'

谁能解释一下为什么第一个语句比第二个慢很多?

最佳答案

Get-ChildItem 是一个提供程序 cmdlet - 这意味着它的大部分实际工作被卸载到底层提供程序,在您的情况下可能是 FileSystem 提供程序。

提供程序本身实际上并不支持 -Include/-Exclude 参数,这是 cmdlet 需要注意的少数事情之一 - 并且对于文件系统提供者这是超重的双重工作,因为 cmdlet 需要通过文件系统层次结构向下递归以确定是否需要基于父目录名称应用排除或包含,您可以 see how this is implemented here .

因此,通过对文件系统提供程序使用 -Include,您要求 PowerShell 执行大量的双重工作。

关于powershell 性能 : Get-ChildItem -Include vs. Get-ChildItem | Where-对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52293871/

相关文章:

parsing - 为什么 PowerShell 将 `$true -or $false` 解析为 `CommandElement`?

powershell - 如何确定 TFS 工作区是本地工作区还是服务器工作区

powershell - 如何验证 PDF 文件是否为 "good"?

powershell - 如何在Powershell中从Get-ChildItem结果中排除项目列表?

powershell - 如何停止异步 PowerShell 脚本 block ?

powershell - 使用嵌套脚本 block 确定问题的范围,然后尝试…最终

regex - 替换参数,删除不应该删除的空格

powershell - while 循环不产生管道输出

python - 为什么 pip 在 PyCharm 之外无法工作

powershell - Powershell Format-Hex不显示行尾。为什么?