You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell ISE与VS Code执行同一段对象选择代码输出不一致咨询

PowerShell 5.1与7.3中-match匹配目录对象的行为差异分析

在PowerShell ISE(PSVersion 5.1.17763.3770)和VS Code中的PowerShell 7.3.3执行以下命令:

Get-ChildItem "$($env:systemdrive)\Program Files (x86)\Windows Kits\10\bin\" -Directory | Where { $_ -match "10." }

出现结果不一致的情况:PowerShell 5.1仅输出名称包含10.的目录,而PowerShell 7.3会包含arm、arm64、x64、x86等看似不符合条件的目录。

这不是PowerShell 7的Bug,而是两个版本对-match处理DirectoryInfo对象的行为发生了变化:

  • PowerShell 5.1中,对DirectoryInfo对象直接使用-match时,默认匹配的是对象的Name属性,只会检查目录名称是否符合正则。
  • PowerShell 7+中,对这类对象使用-match时,会匹配对象的ToString()输出结果,而DirectoryInfo的ToString()返回的是完整路径。那些arm等目录的完整路径里包含Windows Kits\10\,路径字符串中存在10.,因此被误匹配。

要让两个版本执行结果一致,只需明确指定匹配目录的Name属性,修改命令如下:

Get-ChildItem "$($env:systemdrive)\Program Files (x86)\Windows Kits\10\bin\" -Directory | Where { $_.Name -match "10." }

内容的提问来源于stack exchange,提问作者Weishan Yang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 10:12:04