Search notes:

regat.bat

regat stands for registry at.
regat.bat is called with the name of a registry key. It then opens the registry at the specified key using regedit.exe.
The script replaces HKCU with HKEY_CURRENT_USER, HKLM with HKEY_LOCAL_MACHINE and HKCR with HKEY_CLASSES_ROOT.
If regat is called with the special parameter env, it opens the registry at HKEY_CURRENT_USER\Environment.
If called with globenv, it opens the global counterpart at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
If regat is called with the special parameter clsid followed by a GUID (in curly braces), it opens this GUID under HKEY_CLASSES_ROOT\CLSID\{…guid…}.
If called with svc it opens the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services (which stores the Service Control Manager (SCM) database).
If the script is given the special option -m, it opens another regedit Windows. Thus, effectively, without -m it actually passes /m to regedit.exe.
A potentially running process of regedit.exe needs to be killed with taskkill.exe in order for regedit to show the desired key.
@echo off

setlocal enableDelayedExpansion

rem
rem By default, we want a new regedit to be opened
rem
set opt_m=/m

@if [%1] == [-m] (
rem
rem However, if -m is specified, the default of opening a new regedit window
rem is overwritten
rem
  set opt_m=
  shift
)

if [%1] == [] (
  echo Provide a registry key or env
  exit /b
)


@if [%1] == [env] (
    set RegistryKey=HKCU\Environment
    goto goOn
)
@if [%1] == [globenv] (
    set RegistryKey=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    goto goOn
)
@if [%1] == [clsid] (

  if [%2] == [] (
    echo expected: GUID for clsid
    exit /b
  )
  set RegistryKey=HKCR\CLSID\%2
  goto goOn

)
@if [%1] equ [svc] (
  set RegistryKey=HKLM\SYSTEM\CurrentControlSet\Services
  goto goOn
)

:buildRegistryKey
rem
rem Entering a loop to build the registry key
rem from the command line arguments. This loop
rem is necessary because regat.bat might be called
rem with a registry key that contains spaces, such as
rem    regat HKCU\Control Panel\Mouse
rem In such a case, we cannot just assign %1 to !RegistryKey!
rem but must rather iterate over the indivdual
rem arguments and assemble the final registry key.
rem

   if not [%1] == [] (

      if not [!RegistryKey!] == [] (
        set RegistryKey=!RegistryKey! %1
      ) else (
        set RegistryKey=%1
      )
      shift
      goto buildRegistryKey
   )

:goOn


rem
rem Replace HKCU with HKEY_CURRENT_USER etc:
rem
set RegistryKey=!RegistryKey:HKCU=HKEY_CURRENT_USER!
set RegistryKey=!RegistryKey:HKLM=HKEY_LOCAL_MACHINE!
set RegistryKey=!RegistryKey:HKCR=HKEY_CLASSES_ROOT!

if [opt_m] neq [/m] (

rem Kill regedit.exe process if already running.
rem If it's not running, it would write an error message. Redirect
rem the message to nul
    taskkill /f /im regedit.exe 1> nul 2> nul

)

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit /v LastKey /t REG_SZ /d "!RegistryKey!" /f > nul

start regedit %opt_m%

endlocal
Github repository scripts-and-utilities, path: /regat.bat

Windows 10

This script became a bit less important in Windows 10 because regedit.exe now allows to paste a path to a registry key into a text box.

Thanks

2019-09-29: Thanks to Helmut Glatthofer who spotted an unnecessary concatenation of variable values.
This problem is now fixed.

See also

regQuery.bat can be used to query the registry in cmd.exe for a specific value under a given key.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit: LastKey
op-reg-at.pl: a Perl script that does almost the same thing.
The Sysinternals tool regjump is an executable (rather than a batch file) that does pretty much the same thing like regat.bat.
Other scripts

Index