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

PowerShell中为Get-WmiObject输出表Size整数字段拼接GB单位的方法

解决方案

你遇到的报错是因为字符串拼接逻辑没有写在计算属性的表达式脚本块{}内部,导致PowerShell将+"GB"识别为Select-Object的独立位置参数,触发参数匹配错误。

正确修改代码

只需要调整Select-Object中Size对应的计算属性,把单位拼接逻辑放在脚本块内即可,你可以直接用PowerShell内置的1GB常量替代手写的1073741824,可读性更高:

$RAM = Get-WmiObject Win32_PhysicalMemory -ComputerName $Computer |
        select DeviceLocator,Manufacturer,PartNumber, @{n="Size";e={([math]::Truncate($_.Capacity / 1GB)) + "GB"}},Speed | FT -AutoSize
Write-Output $RAM

输出效果

修改后输出的Size列会自动带上GB单位:

DeviceLocator Manufacturer PartNumber         Size Speed
------------- ------------ ----------         ---- -----
DIMM1         000000000000                       8GB  1600
DIMM2         000000000000                       8GB  1600
DIMM3         000000000000                       8GB  1600
DIMM4         000000000000                       8GB  1600

可选优化建议

如果你的PowerShell版本在3.0以上,更推荐使用Get-CimInstance替代已经弃用的Get-WmiObject,兼容性和稳定性更好:

$RAM = Get-CimInstance Win32_PhysicalMemory -ComputerName $Computer |
        select DeviceLocator,Manufacturer,PartNumber, @{n="Size";e={([math]::Truncate($_.Capacity / 1GB)) + "GB"}},Speed | FT -AutoSize

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 04:09:01