Search notes:

regat.ps1

regat.ps1 is a PowerShell script that starts regedit.exe and lets it display a given registry key. This can be achieved by writing the value of the desired key into the value LastKey under the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit (see also Opening the registry at a given key).
The script recognizes the shortcuts
userenv Opens the registry key that stores the personal environment variables.
machineenv Opens the registry key that stores the system environment variables

Example invocations

regat hkcu\SOFTWARE\Classes\.gif
regat userenv
regat machineenv

Source code

#
#   V0.6
#

param (
   [parameter(
        mandatory                  =$true,
        valueFromRemainingArguments=$true
   )]
   [string[]] $regKeyParts
)

set-strictMode -version 3

$regKey = $regKeyParts -join ' '

if ($regKey -eq 'userenv') {
    $regkeyNoColon = 'hkcu\environment'
}
elseif ($regkey -eq 'machineenv') {
    $regkeyNoColon = 'hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
}
else {

   $regKey = $regKey -replace '^Computer\\'          , ''

   $regKey = $regKey -replace '^hkey_local_machine\b', 'hklm'
   $regKey = $regKey -replace '^hkey_current_user\b' , 'hkcu'
   $regKey = $regKey -replace '^hkey_classes_root\b' , 'hkcr'

   $regKey = $regKey -replace '\\$', ''
   $regKey = $regKey -replace '/$' , ''

   $regkeyColon   = $regKey        -replace '^hkcu:?'  , 'hkcu:'
   $regKeyColon   = $regKeyColon   -replace '^hklm:?'  , 'hklm:'
   $regKeyColon   = $regKeyColon   -replace '^hkcr:?'  , 'hkcr:'

   $regKeyNoColon = $regKeyColon   -replace '^hkcu:'   , 'hkcu'
   $regKeyNoColon = $regKeyNoColon -replace '^hklm:'   , 'hklm'
   $regKeyNoColon = $regKeyNoColon -replace '^hkcr:'   , 'hkcr'

   if ($regKeyColon -match '^hkcr') {
      $null = new-psDrive -name hkcr -psProvider registry -root HKEY_CLASSES_ROOT
   }

   if (-not (test-path $regkeyColon) ) {
      write-host "Registry key $regKey does not exist"
      return
   }
}



write-host "open registry at $regKeyNoColon"
if (! (test-path HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit)) {
   write-host "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit does not exit, creating it"
   $null = new-item HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit
}
set-itemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit  LastKey $regKeyNoColon

& regedit -m
Github repository scripts-and-utilities, path: /regat.ps1

History

V0.1 Initial version (2021-08-02)
V0.2 Replace hkey_local_machine with hklm, hkey_current_user with hkcu.
V0.3 Allow opening keys below HKEY_CLASSES_ROOT\ (and add a 'hkcr:' drive if necessary)
V0.4 Use valueFromRemainingArguments attribute on parameter so that registry keys with spaces can be copy-pasted to the command line.
V0.5 Remove leading Computer\ from registry key to be opened. (Computer\ is prepended to a registry key when copying from «address bar» with the registry editor regedit.exe.
V0.6 Create registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit if it does not exist (2022-07-24)

See also

regedit.exe
Other scripts

Index