Add registry values with double quotes or expandable variable data using Reg.exe

The built-in reg.exe console tool lets you add, delete, export or save (as hive) registry keys and values. When adding registry values using the reg.exe tool, you may be wondering how to pass data with double-quotes or environment variables and add it to the registry literally.

This article tells you how to create registry values passing double-quotes and environment variables (without expanding or resolving the folder path) using the reg.exe console tool.

Add a registry value data with double quotes using REG.exe

Consider the following case. If you need to write to the (default) by setting its data to "d:\my projects\runx64.exe" with double quotes, you’ll need to escape the inner double-quotes using a backslash.

Example:

REG ADD HKCR\boxfile\shell\command /ve /t REG_SZ /d "\"d:\my projects\runx64.exe\" "\"%1\""

reg.exe pass double quotes in data and expandable strings

If you don’t escape the inner double-quotes around the data, such as:

REG ADD HKCR\boxfile\shell\command /ve /t REG_SZ /d "d:\my projects\runx64.exe %1"

The command will complete successfully, but you’ll end up creating the following registry key/value without double-quotes:

reg.exe pass double quotes in data and expandable strings

Example 2:

REG ADD HKCR\boxfile\shell\command /ve /t REG_SZ /d "\"d:\my projects\runx64.exe\" -runmax "\"%1\""

reg.exe pass double quotes in data and expandable strings

Add a registry value containing expandable string data using REG.exe

To add a REG_EXPAND_SZ registry value type and pass an expandable string exactly as given (without expanding the variable), you’ll need to use the caret ^ symbol with the % symbol.



Example:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop /t REG_EXPAND_SZ /d ^%USERPROFILE^%\Desktop

reg.exe pass expandable string data without expanding

If you don’t use the caret symbol, as in the following command-line example:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop /t REG_EXPAND_SZ /d %USERPROFILE%\Desktop

The environment variable or expandable string is expanded or resolved to the folder path automatically, as shown in the following image.

reg.exe pass expandable string data without expanding

Hope this article helped you pass complex command-line arguments involving double-quotes and expandable variables to the registry using the reg.exe command-line.


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

Leave a Comment