Reading Registry Values (Workaround)

by Jul 4, 2017

In the previous tip we illustrated that Get-ItemProperty cannot read registry key values when there is a value present with corrupted content:

PS> $key =  "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group  Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0"
PS>  Get-ItemProperty -Path $key
Get-ItemProperty :  Specified cast is not valid.
At line:1 char:1
+ Get-ItemProperty  -Path $key
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:)  [Get-ItemProperty], InvalidCastException
    + FullyQualifiedErrorId :  System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyComma 

As a workaround, you can instead use Get-Item to access the registry key, and then use its .NET members to read all of its values:

$key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0"

$key = Get-Item -Path $key

$hash = @{}
foreach ($prop in $key.Property)
  $hash.$prop = $key.GetValue($prop)



The result looks like this:

Name                           Value                                                                         
----                           -----                                                                         
Extensions                     [{35378EAC-683F-11D2-A89A-00C04FBBCFA2}{0F6B957E-509E-11D1-A7CC-0000F87571E3}]
Link                           Local                                                                         
Options                        0
GPOLink                        1
Version                        65537         
GPOName                        Guidelines of the local group 
lParam                         0     
DSPath                         LocalGPO
FileSysPath                    C:\WINDOWS\System32\GroupPolicy\Machine
DisplayName                    Guidelines of the local group   

Twitter This Tip! ReTweet this Tip!