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

PowerShell:如何将命令输出仅提取目标路径存入变量

PowerShell提取Win32_Product的InstallLocation纯路径值

你遇到的问题是因为Select InstallLocation返回的是包含InstallLocation属性的对象,而非属性的实际值。可以用以下几种方法直接获取纯路径:

  • 方法1:使用-ExpandProperty参数
    修改原命令,通过-ExpandProperty直接提取属性值:

    $InstallLocation1 = Get-WmiObject -Class Win32_Product -Filter 'Name like "%FAS%"' | Select-Object -ExpandProperty InstallLocation
    
  • 方法2:直接访问对象属性
    如果查询结果只有单个匹配项,可直接通过点语法访问属性:

    $InstallLocation1 = (Get-WmiObject -Class Win32_Product -Filter 'Name like "%FAS%"').InstallLocation
    
  • 推荐:使用现代的Get-CimInstance命令
    Get-WmiObject已被PowerShell官方标记为弃用,更推荐使用Get-CimInstance,操作逻辑一致:

    # 用ExpandProperty
    $InstallLocation1 = Get-CimInstance -ClassName Win32_Product -Filter 'Name like "%FAS%"' | Select-Object -ExpandProperty InstallLocation
    
    # 或直接点访问
    $InstallLocation1 = (Get-CimInstance -ClassName Win32_Product -Filter 'Name like "%FAS%"').InstallLocation
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:55:56