Reading Registry Values Fails

by Jul 3, 2017

Occasionally, reading values of a registry key may fail with a strange error message:

 
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 
   nd
 
 
PS> 
 

When this occurs, checking the registry key with regedit.exe reveals that one or more of the values in the key are damaged. In our example, the value “lParam” seems to be corrupted on all Windows machines. Regedit.exe reports “(invalid … value)”.

In this case, Get-ItemProperty will not read any value. You cannot exclude the value:

 
PS>  Get-ItemProperty -Path $key -Include * -Exclude lParam
Get-ItemProperty :  Specified cast is not valid.
At line:1 char:1
+ Get-ItemProperty  -Path $key -Include * -Exclude lParam
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:)  [Get-ItemProperty], InvalidCastException
    + FullyQualifiedErrorId :  System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
 
 
PS>
 

What you can do is read just the values that have valid content:

 
PS> Get-ItemProperty -Path $key -Name DSPath


DSPath       : LocalGPO
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersi
               on\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}\0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersi
               on\Group Policy\History\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}
PSChildName  : 0
PSProvider   : Microsoft.PowerShell.Core\Registry




PS>
 

Twitter This Tip! ReTweet this Tip!