Programmatically open RegEdit to a specific registry path

Here’s a quick PowerShell script to open RegEdit navigated to a specific path in the registry.  This can be handy as sometimes you are given a path but would like to explore/view/modify it in the RegEdit GUI.

We take advantage of the LastKey value to specify the location RegEdit should navigate to when opened, and then start a new instance of RegEdit using the -m switch to bypass the default single-instance behavior.  Without the -m switch, a currently-opened RegEdit will be focused, and the navigation will not occur.

[cmdletbinding()]
param($Key)
 
$Replacements = @{':'='' # Remove PowerShell syntax
                  'HKCU'='HKEY_CURRENT_USER'
                  'HKLM'='HKEY_LOCAL_MACHINE'
                  'HKU'='HKEY_USERS'
                  'HKCC'='HKEY_CURRENT_CONFIG'
                  'HKCR'='HKEY_CLASSES_ROOT'}
$Replacements.Keys | % {
    $key = $key.ToUpperInvariant().Replace($_, $Replacements[$_])
}
Write-Verbose "Open $Key"
sp HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\ -Name LastKey -Value $Key -Force
regedit -m # Open new instance

Paste the above into Open-RegEdit.ps1.  Use it as such:

PS> .\Open-RegEdit.ps1 hkcu:\Software\ODBC -Verbose
VERBOSE: Open HKEY_CURRENT_USER\SOFTWARE\ODBC

And RegEdit will open at the desired location.

May 25, 2013