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

如何从Get-ItemProperty返回的PSCustomObject提取注册表属性名?

提取注册表属性的简洁方案

方法1:直接操作RegistryKey对象(推荐,对齐Get-Item逻辑)

跳过Get-ItemProperty返回的PSCustomObject,用Get-Item拿到注册表键的原生对象后,调用其内置方法直接获取属性名、值和类型:

# 获取注册表键对象
$regKey = Get-Item -Path 'HKLM:\Software\YourTargetKey'

# 拿到所有属性名
$propertyNames = $regKey.GetValueNames()

# 遍历提取每个属性的信息
foreach ($name in $propertyNames) {
    $value = $regKey.GetValue($name)
    $valueKind = $regKey.GetValueKind($name)
    # 输出结构化结果
    [PSCustomObject]@{
        Name  = $name
        Value = $value
        Kind  = $valueKind
    }
}

这种方式和处理文件/文件夹时调用Get-Item后操作FileInfo/DirectoryInfo对象的逻辑完全一致,没有额外的属性过滤成本,代码更直观。

方法2:过滤PSCustomObject的原生属性

如果必须用Get-ItemProperty,可以通过PSObject.Properties筛选真正的注册表属性(排除PowerShell自动添加的成员):

$regItemProps = Get-ItemProperty -Path 'HKLM:\Software\YourTargetKey' |
    Select-Object -ExpandProperty PSObject |
    Select-Object -ExpandProperty Properties |
    # 精准筛选注册表原生属性,不依赖前缀判断
    Where-Object { $_.MemberType -eq 'NoteProperty' -and $_.Value -isnot [System.Management.Automation.PSNoteProperty] }

# 遍历获取属性信息
foreach ($prop in $regItemProps) {
    [PSCustomObject]@{
        Name  = $prop.Name
        Value = $prop.Value
        Kind  = $prop.Value.GetType().Name
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 12:21:03