Getting Registry Values and Value Types

by Apr 15, 2015

Get-ItemProperty can easily read registry values, but you do not get back any information about the registry value type.

Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'

Here is an alternate approach that uses .NET directly, and provides you with full information:

 
PS> Get-RegistryValue 'HKLM\Software\Microsoft\Windows NT\CurrentVersion' 

Name                     Type Value                                      
----                     ---- -----                                      
CurrentVersion         String 6.1                                        
CurrentBuild           String 7601                                       
SoftwareType           String System                                     
CurrentType            String Multiprocessor Free                        
InstallDate             DWord 1326015519                                 
RegisteredOrganization String                                            
RegisteredOwner        String Tobias                                     
SystemRoot             String C:\Windows                                 
InstallationType       String Client                                     
EditionID              String Ultimate                                   
ProductName            String Windows 7 Ultimate                         
ProductId              String 0042xxx657                    
DigitalProductId       Binary {164, 0, 0, 0...}                          
DigitalProductId4      Binary {248, 4, 0, 0...}                          
CurrentBuildNumber     String 7601                                       
BuildLab               String 7601.win7sp1_gdr.150202-1526               
BuildLabEx             String 7601.18741.amd64fre.win7sp1_gdr.150202-1526
BuildGUID              String f974f16b-3e62-4136-a6fb-64fccddecde3       
CSDBuildNumber         String 1130                                       
PathName               String C:\Windows                                 
CSDVersion             String Service Pack 1                                               
 

The work is done by Get-RegistryValue function. Note that this function accepts any valid registry key, and you do not need to use PowerShell drive letters.

function Get-RegistryValue
{
    param
    (
        [Parameter(Mandatory = $true)]
        $RegistryKey
    )

    $key = Get-Item -Path "Registry::$RegistryKey"
    $key.GetValueNames() |
    ForEach-Object {
        $name = $_
        $rv = 1 | Select-Object -Property Name, Type, Value
        $rv.Name = $name
        $rv.Type = $key.GetValueKind($name)
        $rv.Value = $key.GetValue($name)
        $rv
  
    }
}

Twitter This Tip! ReTweet this Tip!