Monday, January 9, 2012

Enumerating Registry Keys

 

Enumerating Registry Keys

To enumerate all subkeys in a Registry key, you might be using a line like this:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Select-Object -expand PSPath

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

It will return the PowerShell-style path including the provider name, not just the Registry path. If you used the -Name parameter, on the other hand, you just get the subkey names and no path at all:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Name

AddressBook

Connection Manager

(...)

The easiest way to get the true Registry path is Resolve-Path:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*

Path

----

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

The resulting paths still use PowerShell drives. To get the native Registry paths, try this:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty ProviderPath

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

Thanks

No comments:

Post a Comment