Get Registry Key Properties from Remote/Deserialized RegistryKey

Welcome Forums General PowerShell Q&A Get Registry Key Properties from Remote/Deserialized RegistryKey

This topic contains 3 replies, has 3 voices, and was last updated by

4 years, 9 months ago.

  • Author
  • #20359

    Topics: 7
    Replies: 38
    Points: 55
    Rank: Member

    I'm using Invoke-Command to read a registry key on a remote computer. I get some deserialized RegistryKey objects back, but can't seem to access the key properties. The Format-Table cmdlet shows the property names/values, but I can't find the property I use to access them in my script.

    > $k = Invoke-Command -ComputerName ******** -ScriptBlock { Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' } 
    > $k | Format-Table
        Hive: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
    Name                           Property                                                                                                              PSComputerName                                                                                                       
    ----                           --------                                                                                                              --------------                                                                                                       
    {FFC6E93A-B9AD-3F20-9B06-EE20E AuthorizedCDFPrefix :                                                                                                 ********                                                                                                     
    24AAEAF}                       Comments            :                                                                                                                                                                                                                      
                                   Contact             :                                                                                                                                                                                                                      
                                   DisplayVersion      : 11.0.51106                                                                                                                                                                                                           
                                   HelpLink            :                                                                                                                                                                        
                                   HelpTelephone       :                                                                                                                                                                                                                      
                                   InstallDate         : 20140319                                                                                                                                                                                                             
                                   InstallLocation     :                                                                                                                                                                                                                      
                                   InstallSource       : C:\ProgramData\Package                                                                                                                                                                                               
                                   ModifyPath          : MsiExec.exe /X{FFC6E93A-B9AD-3F20-9B06-EE20E24AAEAF}                                                                                                                                                                 
                                   NoModify            : 1                                                                                                                                                                                                                    
                                   NoRepair            : 1                                                                                                                                                                                                                    
                                   Publisher           : Microsoft Corporation                                                                                                                                                                                                
                                   Readme              :                                                                                                                                                                                                                      
                                   Size                :                                                                                                                                                                                                                      
                                   EstimatedSize       : 704660                                                                                                                                                                                                               
                                   SystemComponent     : 1                                                                                                                                                                                                                    
                                   UninstallString     : MsiExec.exe /X{FFC6E93A-B9AD-3F20-9B06-EE20E24AAEAF}                                                                                                                                                                 
                                   URLInfoAbout        :                                                                                                                                                                                                                      
                                   URLUpdateInfo       :                                                                                                                                                                                                                      
                                   VersionMajor        : 11                                                                                                                                                                                                                   
                                   VersionMinor        : 0                                                                                                                                                                                                                    
                                   WindowsInstaller    : 1                                                                                                                                                                                                                    
                                   Version             : 184600482                                                                                                                                                                                                            
                                   Language            : 1033                                                                                                                                                                                                                 
                                   DisplayName         : Microsoft Visual C++ 2012 Core Libraries   

    However, when I use Get-Member I don't see any way to get the property values:

    > $k | Get-Member * -Force
       TypeName: Deserialized.Microsoft.Win32.RegistryKey
    Name               MemberType   Definition                                                                                                                                                                                                                                
    ----               ----------   ----------                                                                                                                                                                                                                                
    pstypenames        CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]] pstypenames{get=PSTypeNames;}                                                                   
    psadapted          MemberSet    psadapted {SubKeyCount, View, Handle, ValueCount, Name}                                                                                                                                                                                   
    psbase             MemberSet    psbase {}                                                                                                                                                                                                                                 
    psextended         MemberSet    psextended {ToString, Property, PSPath, PSParentPath, PSChildName, PSDrive, PSProvider, PSIsContainer, PSComputerName, RunspaceId, PSShowComputerName}                                                                                    
    psobject           MemberSet    psobject {BaseObject, Members, Properties, Methods, ImmediateBaseObject, TypeNames, get_Members, get_Properties, get_Methods, get_ImmediateBaseObject, get_BaseObject, get_TypeNames, ToString, Copy, CompareTo, Equals, GetHashCode, G...
    ToString           Method       string ToString(), string ToString(string format, System.IFormatProvider formatProvider), string IFormattable.ToString(string format, System.IFormatProvider formatProvider)                                                              
    Property           NoteProperty Deserialized.System.String[] Property=                                                                                                                                                                                                    
    PSChildName        NoteProperty System.String PSChildName=AddressBook                                                                                                                                                                                                     
    PSComputerName     NoteProperty System.String PSComputerName=build01d-whs-04                                                                                                                                                                                              
    PSDrive            NoteProperty Deserialized.System.Management.Automation.PSDriveInfo PSDrive=HKLM                                                                                                                                                                        
    PSIsContainer      NoteProperty System.Boolean PSIsContainer=True                                                                                                                                                                                                         
    PSParentPath       NoteProperty System.String PSParentPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall                                                                                                     
    PSPath             NoteProperty System.String PSPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook                                                                                               
    PSProvider         NoteProperty Deserialized.System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.Core\Registry                                                                                                                                      
    PSShowComputerName NoteProperty System.Boolean PSShowComputerName=True                                                                                                                                                                                                    
    RunspaceId         NoteProperty System.Guid RunspaceId=49c33ad2-07c3-4f9b-aba8-7452e7df86cb                                                                                                                                                                               
    Handle             Property     System.String {get;set;}                                                                                                                                                                                                                  
    Name               Property     System.String {get;set;}                                                                                                                                                                                                                  
    SubKeyCount        Property     System.Int32 {get;set;}                                                                                                                                                                                                                   
    ValueCount         Property     System.Int32 {get;set;}                                                                                                                                                                                                                   
    View               Property     System.String {get;set;}    

    Does anyone know how I can read the property values of a deserialized Microsoft.Win32.RegistryKey object?

  • #20361

    Topics: 12
    Replies: 22
    Points: -16
    Rank: Member

    Remove the format-table, then run get-member and see if you get the properties. I had a similar issue in a recent script and for some reason the formatting removed my ability to access properties.

  • #20363

    Topics: 7
    Replies: 38
    Points: 55
    Rank: Member

    See the second code block in my original post. It shows that I'm calling Get-Member directly on the object without going through Format-Table.

  • #20374

    Topics: 1
    Replies: 64
    Points: 0
    Rank: Member

    You are actually getting tricked by Format-Table and it shows the values of your local registry.

    The formatdata for the RegistryKey type is a scriptblock containing this code:

    $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 += "..." }

    To demonstrate this I made a simple script to be run in a lab environment:

    $Remote = 'Server02'
    $SetScript = {
        $Key = New-Item -Path 'HKLM:\SOFTWARE\TestKey\1' -Force
        New-ItemProperty -Path $Key.PSPath -PropertyType String -Name Computer -Value $ENV:ComputerName -Force | Out-Null
    # This will set the regkey on the local machine
    # This will set the regkey on the remote machine
    Invoke-Command -ComputerName $Remote -ScriptBlock $SetScript
    $k = Invoke-Command -ComputerName $Remote -ScriptBlock { Get-ChildItem 'HKLM:\Software\TestKey' }
    $k | Format-Table

    This script will create a registry string value named Computer containing the computername of the Machine both on the local machine (Server01 in my case) and the remote Machine (Server02 in my case) in the path 'HKLM:\Software\TestKey\1'.
    Then it will run your command to get the childitems from 'HKLM:\Software\TestKey'

    On the last row, piping $k to Format-Table will output "Computer : Server01" in the Property column.

The topic ‘Get Registry Key Properties from Remote/Deserialized RegistryKey’ is closed to new replies.