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
相关产品推荐
相关产品推荐

