#PSTip Access remote registry using PowerShell

Using the Microsoft.Win32.Registry class it is possible to access both–the local registry and, more importantly, the registry of a remote system. Using the PowerShell cmdlet this is unfortunately not possible. Ravikanth posted a similar tip in PowerShell Magazine in which he uses it to retrieve SQL instance names using remote registry.

The following code will retrieve a list of sub keys from the HKLM:\Software path in the registry on Server1 computer:

1
2
3
4
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,Server1) 

$RegSubKey = $Reg.OpenSubKey("System\CurrentControlSet")
$Values = $RegSubKey.GetSubKeyNames()

Using this information it is possible to extract a value from the subkey as well. The following code sample will extract the path parameter from the first subkey in the Software container.

1
2
$RegPath = Join-Path 'Software' $Values[1]
$Reg.OpenSubKey($RegPath).GetValue('Path') 

For more information about this class and the available methods please refer to the MSDN article:

RegistryKey Class

Share on: