How to use REG_EXPAND_SZ from the commandline?

2019-02-18 09:33发布

I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the "reg add" command.

reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "%systemroot%\system32"

Now, I don't know how this was designed to work. When I invoke the command above, the variable %systemroot% gets expanded to C:\Windows. I've tried the following not to make the variable to expand, but there is no way I could force it not to:

  • escaping the `%%`'s with an `%, ^, \` - doesn't work even if I use double quotes around
  • using the single quotes '' around the whole /d string
  • use `setlocal setdelayedexpansion`? sth like:

# (setlocal enabledelayedexpansion) && (reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "!systemroot!\system32") && (setlocal disabledelayedexpansion)

The variable 'data' (/d) field is either like ^%systemroot^% or like !systemroot! or just expands to C:\windows . I could probably use the .reg file to accomplish my task, but I simply don't want to do it.
I thought that maybe there is something wrong with the program that I use to display the variable contents (regedit / regedt32 / reg query (commandline)), but after checking this is probably not the case.
Any ideas? I'm mainly interested how the variable value should look like in the regedit window, should it be like :"%systemroot%\system32" or "C:\windows\system32" to be properly expanded by other programs. Regards.

9条回答
在下西门庆
2楼-- · 2019-02-18 10:01

We have a use in an autounattend.xml. I was first following aphoria's answer, then I had to use mivk's enhanced answer. I had it working. Then I made a small change and it stopped working. I consider this insight worth reporting, to save others lengthy frustration.

Building on previous answers, here is how to make this work in autounattend.xml:

<CommandLine>cmd.exe /c reg.exe add HKLM\ourkey /v ourvalue /t REG_EXPAND_SZ /d "our data with spaces"^%USERNAME^%"and more spaces" /f</CommandLine>

What matters is the cmd.exe /c. If you don't use cmd.exe then reg.exe still runs, but it puts literally ^% into the registry, not the desired %.

Maybe the answer by ASTX813 would work, but this seems easier to read.

查看更多
SAY GOODBYE
3楼-- · 2019-02-18 10:06

without quotes

reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "%%%^%systemroot%%%^%\system32"

with quotes

reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "\"^%%systemroot^%%\system32\""
查看更多
倾城 Initia
4楼-- · 2019-02-18 10:08

From a command line, this worked for me:

reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot^%\system32
查看更多
forever°为你锁心
5楼-- · 2019-02-18 10:08

From a batch file you end up with problems with quotes, which you can then escape with a backslash, for example:

reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%name%" /v "UninstallString" /t REG_EXPAND_SZ /d "\"%%comspec%%\" /d /c \"%dest%\uninstall.cmd\""
查看更多
趁早两清
6楼-- · 2019-02-18 10:09

I recognize that this is an old thread, but since there doesn't seem to be an answer and I too was driven crazy by this problem, I'll share what I've come up with. It's convoluted, it's ugly, and it looks like this:

SET VAR=% SET SET VALUE=%VAR%SystemRoot%VAR%\system32... REG ADD HKLM... /v Test /t REG_EXPAND_SZ /d %VALUE%

I end up with Test containing the string %SystemRoot%\system32

Edit: Should have finished testing before posting. While this worked from the command line, it failed in my command scripts. What DID work was SET VALUE=%%SystemRoot%%\system32...

查看更多
Summer. ? 凉城
7楼-- · 2019-02-18 10:13

There's no magic here, you must escape every % in command-line with/become: %%.

In batch file, you again must escape them, become %%%%SystemRoot%%%%. Yes, four ampersands. It's ugly, but it's the way it is, and it's pretty consistent though.

note: Using ^ to escape it as literal character might help/cleanup in one step/degree only (as long as it's not interpreted). The advantage using ^ is that you can write in command-line or batch file with identical syntax: eg. "^%SytemRoot^%", since in both environments, % treated equally as literal.

查看更多
登录 后发表回答