In powershell, it's possible to get an array of RegistryKeys
as follows:
$hkeys = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
When I inspect the first element of this array, this is what I get:
Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Name Property
---- --------
7-Zip DisplayName : 7-Zip 21.03 beta (x64)
DisplayVersion : 21.03 beta
DisplayIcon : C:\Program Files\7-Zip\7zFM.exe
InstallLocation : C:\Program Files\7-Zip\
UninstallString : "C:\Program Files\7-Zip\Uninstall.exe"
NoModify : 1
NoRepair : 1
EstimatedSize : 5237
VersionMajor : 21
VersionMinor : 3
Publisher : Igor Pavlov
Property
seemed a little strange, so I looked further into that:
> $hkeys[0].property.gettype
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
The elements in the property
attribute, since they are delimited by a colon :
did not seem like strings, so I looked a bit further, but found that they are indeed String
objects:
> $hkeys[0].property[0].gettype
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Since they appeared to be string objects, I tried to echo the first one. However, it only shows the first part of the string and not the part after the colon:
> $hkeys[0].property[0]
DisplayName
I feel like there's something fundamental which I don't understand here. Are the elements of the array really String
objects? If so, why won't the part after the colon appear?
CodePudding user response:
Registry objects have a defined output format which powershell uses when no format is given. You can read more here about_Format.ps1xml
You can test this by calling
$hkeys #formated with name:value, actually uses $hkeys | Out-Default
$hkeys | Format-Table Property #value won't show anymore
$hkeys | Format-List #value won't show anymore
The default format file for registry (ex: C:\Windows\System32\WindowsPowerShell\v1.0\Registry.format.ps1xml) will display Property as following
$result = (Get-ItemProperty -LiteralPath $_.PSPath |
Select * -Exclude PSPath,PSParentPath,PSChildName,PSDrive,PsProvider |
Format-List | Out-String | Sort).Trim()
$result = $result.Substring(0, [Math]::Min($result.Length, 5000) )
if($result.Length -eq 5000) { $result = "..." }
$result
and as you've noticed, the output is a string[]
To get the actual value in powershell you need to call a method or use Get-ItemProperty
$hkeys[0].getvalue('DisplayName') #you have to specify the property name
# or
$hkeys[0] | Get-ItemProperty