REG ADD REG_SZ where Value contains embedded double quotes

batchbatch filecommand linewindowswindows-registry

This is driving me bananas but it must be something very simple. I am trying to add an ImagePath Value (REG_SZ) in a Batch script using REG ADD, where the Value Data contains embedded "double quotes". But I keep getting an "Invalid Syntax" error. This is the Value I am trying to add:

Key  : HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS
Value: ImagePath REG_SZ
Data : "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS 

I tried enclosing the Data in single quotes:

@SETLOCAL
@SET IPATH="C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS
@REG.EXE ADD "HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS" /f /v ImagePath /t REG_SZ /d '%IPATH%'

I also tried enclosing in "double quotes", bang (!), [brackets], \backslashes\ and ^carets^ but I keep getting the syntax error.

Any idea what I am doing wrong?

Best Answer

The parameter needs to be double-quoted. Other randomly chosen punctuation (single quotes, exclamation marks, ...) is not going to work.

However, you need to prefix the inner double-quotes with backslashes:

@SET ImagePath=\"C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe\" -sSQLEXPRESS
@REG.EXE ADD "HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS" /f /v ImagePath /t REG_SZ /d "%ImagePath%"

Note 1: To assign values in Cmd you need to use SET (as in SET VAR=VALUE).

Note 2: Do not use PATH as the variable name, as it is already used by Cmd for specifying program locations and overwriting it will make the script unable to run REG.EXE.

Related Question