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

使用PowerShell的Get-WmiObject查询指定软件时遇命令识别错误

PowerShell筛选特定软件详情的问题解决

错误原因及修正

你的代码存在两个关键问题:

  1. 属性拼写错误:$._name多了一个点,正确的对象属性引用格式是$_.Name
  2. 比较逻辑错误:用-EQ和数组$SoftWare直接比较无法匹配,应该用-in运算符判断当前软件名称是否在目标列表中

另外补充两个重要提示:Get-WmiObject已被PowerShell官方弃用,建议替换为Get-CimInstance;Win32_Product类在调用时可能触发MSI安装程序的自动修复操作,存在不必要的风险,更推荐通过注册表查询已安装软件信息。

修正后的基础版代码

$SoftWare = @(
    'Office 16 Click-to-Run Licensing Component',
    'Adobe Acrobat',
    'Google Chrome',
    'Local Administrator Password Solution',
)

# 修正属性引用和比较逻辑,同时替换为推荐的Get-CimInstance
Get-CimInstance -Class Win32_Product | Where-Object {$_.Name -in $SoftWare}

更安全的替代方案(查询注册表)

$SoftWare = @(
    'Office 16 Click-to-Run Licensing Component',
    'Adobe Acrobat',
    'Google Chrome',
    'Local Administrator Password Solution',
)

# 覆盖32位和64位软件的注册表路径
$regPaths = @(
    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
    'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)

Get-ItemProperty -Path $regPaths | 
    Where-Object { $_.DisplayName -in $SoftWare -and $_.DisplayName } |
    Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 20:03:22