Get-ChildItem 与直接使用 .NET Framework 的 [System.IO.Directory]::GetFiles() 方法和 UNC 路径

Get-ChildItem vs. direct use of the .NET Framework's [System.IO.Directory]::GetFiles() method with UNC paths

当使用 UNC 路径(例如:\machine\share\dir)时,我可以从 UNC 路径使用 dir 命令(Get-ChildItem)时获取文件列表,但是如果我尝试 [System.IO.Directory]::GetFiles,我得到一个空列表(不是错误,只是没有项目)。我认为 PS 是基于 .NET 框架构建的。有谁知道为什么 Get-ChildItem 可以使用 UNC 路径,而 .NET GetFiles 方法 returns 是一个空列表?

[System.IO.Directory]::GetFiles() returns 仅 文件,而 Get-ChildItem(及其内置别名 dir) 默认情况下 returns 两个文件 目录。

因此,在具有子目录的目录上调用[System.IO.Directory]::GetFiles()(没有文件 ) 产生 "nothing" (一个空字符串数组)).

另一种说法,粗略地说:Get-ChildItem[System.IO.Directory]::GetFiles() (akin to Get-ChildItem -File, PSv3+) and [System.IO.Directory]::GetDirectories() (akin to Get-ChildItem -Directory, PSv3+), or, more directly, the counterpart to [System.IO.Directory]::GetFileSystemEntries()联合

另一种选择是使用 .NET 4+(在 PSv3+ 中可用)文件系统项 枚举 API,例如我的 [System.IO.Directory]::EnumerateFileSystemInfos(). For an example, see this answer

Get-ChildItem 和直接使用 [System.IO.Directory] 类型之间有许多具体差异,但是,值得注意的是 Get-ChildItem returns objects 而不是字符串,并且 Get-ChildItem 默认跳过 隐藏的 项(必须使用 -Force)。

通常情况下,选择使用 PowerShell 自己的 cmdlet 还是直接使用 .NET Framework 是便利性和性能之间的权衡。
后者通常会更快,并且在手头的情况下,对于 PowerShell 版本 1 和 2 中的 UNC 路径尤其如此 - 请参阅 this blog post

感谢 wOxxOm and Mike Sherrill 'Cat Recall' 补充信息。