Discussion
Although PowerShell does not directly let you access and manipulate the registry of a remote computer, it still supports this by working with the .NET Framework. The functionality exposed by the .NET Framework is a bit more developeroriented than we want, so we can instead use a script to make it easier to work with.
Example 187 lets you get the properties (or a specific property) from a given remote registry key. In order for this script to succeed, the target computer must have the remote registry service enabled and running.
Example 187. GetRemoteRegistryKeyProperty.ps1
############################################################################## ## ## GetRemoteRegistryKeyProperty.ps1 ## ## Get the value of a remote registry key property ## ## ie: ## ## PS >$registryPath = ## "HKLM:\software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" ## PS >GetRemoteRegistryKeyProperty LEEDESK $registryPath "ExecutionPolicy" ##
Example 187. GetRemoteRegistryKeyProperty.ps1 (continued)
##############################################################################
param( $computer = $(throw "Please specify a computer name."), $path = $(throw "Please specify a registry path"), $property = "*" )
## Validate and extract out the registry key if($path match "^HKLM:\\(.*)") {
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
"LocalMachine", $computer) } elseif($path match "^HKCU:\\(.*)") {
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
"CurrentUser", $computer) } else {
WriteError ("Please specify a fullyqualified registry path " + "(i.e.: HKLM:\Software) of the registry key to open.") return }
## Open the key $key = $baseKey.OpenSubKey($matches[1]) $returnObject = NewObject PsObject
## Go through each of the properties in the key foreach($keyProperty in $key.GetValueNames()) {
## If the property matches the search term, add it as a ## property to the output if($keyProperty like $property) {
$returnObject | AddMember NoteProperty $keyProperty $key.GetValue($keyProperty) } }
## Return the resulting object $returnObject
## Close the key and base keys $key.Close() $baseKey.Close()