PoshCode Logo PowerShell Code Repository

Get-RemoteRegistry by Joel Bennett 17 months ago (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2103"></script>download | new post

A script to do a query on a remote registry key or value.
Because the Registry provider inexplicably doesn’t support it.

  1. #.SYNOPSIS
  2. #  Query the registry on a remote machine
  3. #.NOTE
  4. #  You have to have access, and the remote registry service has to be running
  5. #
  6. # Version History:
  7. #   3.0
  8. #       + updated to PowerShell 2
  9. #       + support pipeline parameter for path
  10. #   2.1
  11. #       + Fixed a pasting bug
  12. #       + I added the "Properties" parameter so you can select specific redgistry values
  13. # ######################################################################################
  14. #.EXAMPLE
  15. #      (Get-RemoteRegistry ${Env:ComputerName} HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\).Subkeys |
  16. #         Get-RemoteRegistry ${Env:ComputerName} `
  17. #         -Path { "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$_" } `
  18. #         -Properties DisplayName, DisplayVersion, Publisher, InstallDate, HelpLink, UninstallString
  19. #.EXAMPLE
  20. #   Get-RemoteRegistry $RemotePC "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP"
  21. #     * Returns a list of subkeys (because this key has no properties)
  22. #.EXAMPLE
  23. #   Get-RemoteRegistry $RemotePC "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727"
  24. #     * Returns a list of subkeys and all the other "properties" of the key
  25. #.EXAMPLE
  26. #   Get-RemoteRegistry $RemotePC "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727\Version"
  27. #     * Returns JUST the full version of the .Net SP2 as a STRING (to preserve prior behavior)
  28. #.EXAMPLE
  29. #   Get-RemoteRegistry $RemotePC "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" Version
  30. #     * Returns a custom object with the property "Version" = "2.0.50727.3053" (your version)
  31. #.EXAMPLE
  32. #   Get-RemoteRegistry $RemotePC "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" Version,SP
  33. #     * Returns a custom object with "Version" and "SP" (Service Pack) properties
  34. #
  35. #  For fun, get all .Net Framework versions (2.0 and greater)
  36. #  and return version + service pack with this one command line:
  37. #
  38. #    Get-RemoteRegistry $RemotePC "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP" |
  39. #    Select -Expand Subkeys | ForEach-Object {
  40. #      Get-RemoteRegistry $RemotePC "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\$_" Version,SP
  41. #    }
  42. param(
  43.     [Parameter(Position=0, Mandatory=$true)]
  44.     [string]$computer = $(Read-Host "Remote Computer Name")
  45. ,
  46.     [Parameter(Position=1, ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true, Mandatory=$true)]
  47.     [string]$Path     = $(Read-Host "Remote Registry Path (must start with HKLM,HKCU,etc)")
  48. ,
  49.     [Parameter(Position=2)]
  50.     [string[]]$Properties
  51. )
  52. process {
  53.    $root, $last = $Path.Split("\")
  54.    $last = $last[-1]
  55.    $Path = $Path.Substring($root.Length + 1,$Path.Length - ( $last.Length + $root.Length + 2))
  56.    $root = $root.TrimEnd(":")
  57.  
  58.    #split the path to get a list of subkeys that we will need to access
  59.    # ClassesRoot, CurrentUser, LocalMachine, Users, PerformanceData, CurrentConfig, DynData
  60.    switch($root) {
  61.       "HKCR"  { $root = "ClassesRoot"}
  62.       "HKCU"  { $root = "CurrentUser" }
  63.       "HKLM"  { $root = "LocalMachine" }
  64.       "HKU"   { $root = "Users" }
  65.       "HKPD"  { $root = "PerformanceData"}
  66.       "HKCC"  { $root = "CurrentConfig"}
  67.       "HKDD"  { $root = "DynData"}
  68.       default { return "Path argument is not valid" }
  69.    }
  70.  
  71.  
  72.    #Access Remote Registry Key using the static OpenRemoteBaseKey method.
  73.    Write-Verbose "Accessing $root from $computer"
  74.    $rootkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($root,$computer)
  75.    if(-not $rootkey) { Write-Error "Can't open the remote $root registry hive" }
  76.  
  77.    Write-Verbose "Opening $Path"
  78.    $key = $rootkey.OpenSubKey( $Path )
  79.    if(-not $key) { Write-Error "Can't open $($root + '\' + $Path) on $computer" }
  80.  
  81.    $subkey = $key.OpenSubKey( $last )
  82.    
  83.    $output = new-object object
  84.  
  85.    if($subkey -and $Properties -and $Properties.Count) {
  86.       foreach($property in $Properties) {
  87.          Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property)
  88.       }
  89.       Write-Output $output
  90.    } elseif($subkey) {
  91.       Add-Member -InputObject $output -Type NoteProperty -Name "Subkeys" -Value @($subkey.GetSubKeyNames())
  92.       foreach($property in $subkey.GetValueNames()) {
  93.          Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property)
  94.       }
  95.       Write-Output $output
  96.    }
  97.    else
  98.    {
  99.       $key.GetValue($last)
  100.    }
  101. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me