May I see your batch file?
I went ahead with an improvement to my previous solution because my last few posts refreshed my mind about the subject.
- I've tested the revised solution in both Windows 10 & Windows 11.
- My tests have included using spaces & ampersands in each variable.
- I have not written much error-checking into the scripts.
This revised solution also make uses of the
LastKey facility of RegEdit.
My batch file writes into the Registry Key
HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit
to set the item
LastKey
in order to trick the Registry into thinking that my desired Key was the last one selected before the Registry was last closed and is therefore the Key that RegEdit will show upon opening.
This revised solution is not as neat as a PS solution could be.
1 When I want a shortcut to a Registry Key of interest I click on a shortcut,
RegEdit - make new ShowRegKey shortcut, that exists in my menu system.
2 The shortcut,
RegEdit - make new ShowRegKey shortcut, runs a VBS script to create the shortcut I need, which it places on my Desktop.
The VBS script asks me for:
2.1 The address of the Registry Key I want to open
e.g. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
2.2 The name I want for the shortcut
e.g.
RegEdit shortcut - UserShellFolders
2.3 The shortcut description [also called its Comment field and often referred to as its popup description]
e.g.
Link to Registry UserShellFolders Key
The shortcut is created on my Desktop, which is my clearing house / work-in-progress location, and I move it to whichever folder I want to keep it in. The one made using the inputs shown above, just for example, is moved to the folder that I keep all my UserAccounts/UserFolders info in so if I'm working on user folder setting up or user folder faults I can use the shortcut to get straight to the relevant Registry Key.
RegEdit - make new ShowRegKey shortcut.vbs
Code:
' This tool creates a shortcut that will open the Registry at a chosen Key
' This tool was put together using VBS Help [2001 version] and [for the Arguments property] https://docs.microsoft.com/en-GB/troubleshoot/windows-client/admin-development/create-desktop-shortcut-with-wsh
''''''''''''''' Set the required variables '''''''''''''''
''''' RegistryKeyPath '''''
RegistryKeyPath = InputBox("Enter the chosen Registry address for the shortcut to point to [in full or using HKCU etc]", "1/3 Create a Registry address shortcut")
' check that the InputBox dialog has not been cancelled
If (RegistryKeyPath = "") Then
WScript.Quit
End If''''' RegistryShortcutName '''''
RegistryShortcutName = InputBox("Enter a name for the shortcut", "2/3 Create a Registry address shortcut")
' check that the InputBox dialog has not been cancelled
If (RegistryShortcutName = "") Then
WScript.Quit
End If''''' RegistryShortcutDescription '''''
RegistryShortcutDescription = InputBox("Enter an optional description for the shortcut [its popup]", "3/3 Create a Registry address shortcut")
' check that the InputBox dialog has not been cancelled
If (RegistryShortcutName = "") Then
WScript.Quit
End If
''''''''''''''' Create the shortcut '''''''''''''''
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set objShellLink = WshShell.CreateShortcut(strDesktop & "\" & RegistryShortcutName & ".lnk")
objShellLink.TargetPath = "C:\Tools\Registry\ShowRegKey.bat"
objShellLink.Arguments = Chr(34) & RegistryKeyPath & Chr(34)
objShellLink.WindowStyle = 7
objShellLink.IconLocation = "%SystemRoot%\regedit.exe, 0"
objShellLink.Description = RegistryShortcutDescription
objShellLink.WorkingDirectory = strDesktop
objShellLink.Save
3 The shortcut runs, and passes the chosen parameter to, the batch file
ShowRegKey.bat and this sets the
HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit
entry
LastKey.
This batch file lives with all my other scripts within my folder C:\Tools - see
Set up my Tools folder ditty - TenForums
ShowRegKey.bat
Code:
:: This takes the Registry key passed by the shortcut to this batch file, opens RegEdit [for which Admin permission may be requested], goes to the specified Registry key [if it exists]
:: Based on an idea in https://ss64.com/nt/regedit.html
::prompt $g
Set "ThisKey=%~1"
::Bail out if no argument has been passed
If "FF%ThisKey%FF"=="FFFF" GoTo Abort
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%ThisKey%" /f
:: Always open the Registry maximised
Start /MAX /b regedit
:Abort
Denis