Search code examples
batch-filewindows-10registry

Windows Batch script to add entry to Registry


I'm attempting to create a batch script that would reproduce an entry in the registry I created manually. The script is going to be part of the installation process for a small WEB application I'm working on.

The needed entry into the registry is in fact the definition of a custom protocol to be invoked from JavaScript to activate an external process (in this case, a PowerShell script).

The manually generate entry is shown in the following snapshot and it works as needed:

enter image description here

The script I have so far correctly replicates the shown entry except for the last one (the command part) is complaining about a syntax error. Here is the piece of script hat attempts to register the last part of the definition and is triggering the error message:

set scriptpath=%~dp0
reg query HKCR\fddmyalbs2\shell\open\command
set Query_Result=%errorlevel%

if "%Query_Result%"=="0" (
    echo "HKCR\fddmyalbs2\shell\open\command OK!"
) else (
    echo "HKCR\fddmyalbs2\shell\open\command missing. Registering..."
    reg add HKCR\fddmyalbs2\shell\open\command /t REG_SZ /d "powershell.exe" %scriptpath%FD_Albums_MNGR.ps1 "^%1" /f
)

What is syntax error (in the reg add command)?


Solution

  • The entire thing can be achieved with a single line batch file:

    @%SystemRoot%\System32\reg.exe Query "HKCU\SOFTWARE\Classes\fdmyalbs\shell\open\command" 1>NUL 2>&1 && (Echo HKCU\SOFTWARE\Classes\fdmyalbs\shell\open\command OK!& %SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1>NUL) || (Echo HKCU\SOFTWARE\Classes\fdmyalbs\shell\open\command missing. Registering...& %SystemRoot%\System32\reg.exe Add "HKCU\SOFTWARE\Classes\fdmyalbs\shell\open\command" /VE /D "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -File \"%~dp0MyAlbums\FD_Albums_MNGR.ps1\" \"%%~1\"" 1>NUL)
    

    Please note, this uses the information in your registry editor image, and not the modified information in your batch file, (which does not match that). Please do not change the registry root key and subkeys, the ones I have provided are correct, (you should not be using the HKCR root key, which is essentially an amalgamated view of both HKLM\SOFTWARE\Classes and HKCU\SOFTWARE\Classes). If this is really for all users of the computer, you may replace all instances of HKCU with HKLM, but please note that the script must then be run elevated. Additionally I have improved your PowerShell command, so I'd advise you to leave that alone too.


    If you wanted it to be a little more readable then you could split the line over several instead:

    @Echo Off
    SetLocal EnableExtensions DisableDelayedExpansion
    
    Set "RegKey=HKCU\SOFTWARE\Classes\fdmyalbs\shell\open\command"
    
    %SystemRoot%\System32\reg.exe Query "%RegKey%" 1>NUL 2>&1 && (
        Echo %RegKey% OK!
        %SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1>NUL
    ) || (
        Echo %RegKey% missing. Registering...
        %SystemRoot%\System32\reg.exe Add "%RegKey%" /VE /D "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -File \"%~dp0MyAlbums\FD_Albums_MNGR.ps1\" \"%%~1\"" 1>NUL
    )