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

