在 PSv3+ 中,但在 PowerShell Core v6.x 中不起作用,在 v7 中已修复(请参阅 this GitHub issue):
Get-ChildItem -File -Filter *.
出于性能原因,
-Filter 通常比-Include / -Exclude 更可取,因为它在源头进行过滤,而不是返回所有对象并让PowerShell进行过滤。
在 PSv2 中,-File 开关不可用,您需要额外调用 Where-Object 以将结果限制为文件,如 TheIncorrigible1指出:
Get-ChildItem -Filter *. | Where-Object { -not $_.PSIsContainer }
较慢的 PowerShell Core 解决方案:
Get-ChildItem -File | Where-Object -Not Extension
可选背景信息:
-Filter 参数由底层提供程序处理,而不是由 PowerShell,这意味着它的行为可能与 PowerShell 不同,这里确实是这种情况:文件系统提供程序使用 Windows API 的通配符表达式匹配,它的功能比 PowerShell 的少,还有一些历史怪癖;此外,它仅限于单个通配符表达式,而-Include / -Exclude 支持多个通配符(以, 分隔)。
然而,-Filter 提供了 PowerShell 通配符匹配所不具备的功能:使用 *. 匹配不带扩展名的文件/目录。
-Include / -Exclude 通常以牺牲性能为代价提供功能优势,但它们也有自己的局限性和怪癖:
简而言之:-Include / -Exclude 在这里不提供任何解决方案。