开发者

How to use REG_EXPAND_SZ from the commandline?

开发者 https://www.devze.com 2023-01-14 05:31 出处:网络
I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the \"reg add\" command.

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 (reged开发者_Go百科it / 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.


From a command line, this worked for me:

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


The syntax suggested by aphoria is correct, without quotes as you discovered. But you run into trouble when you need spaces in the value.

However, it is possible to mix-in quotes in the value. It looks weird, but it works:

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d ^%SystemRoot^%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ

You can use quotes where needed, just not around your variables. And not right after a backslash.

Also, beware that the syntax for use in a batch file is not the same as on the command line. If you put that line into a batch file, you need to replace the "^" with another "%":

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d %%SystemRoot%%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ

(cmd.exe always reminds me of Henry Spencer's quote "Those who do not understand Unix are condemned to reinvent it, poorly")


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...


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.


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.


The answer is very simple. Only quote the spaces and escape the %'s.

In command line, escape the %'s with the ^.

reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "^%username^% /f

In batch file, escape the %'s with another %.

reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun  /t REG_SZ /d title" "%%username%% /f

This took me a while to figure out. In my mind, I should quote the entire string, but could not get the variable to be passed without expanding it. I broke my way of thinking by quoting spaces instead.


Please try the following:

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


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\""


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\""


I finally got sick of the banner/logo on CScript and decided to prefix VBS associations with //nologo in my 'new build' batch script. This required modifying a REG_EXPAND_SZ registry entry with an environment variable and this question and its various answers were the best inspiration I could find. Here's the final script.

SET "thecmd=\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*"
ECHO.%thecmd%
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "%thecmd%" /f
SET thecmd=

Storing the command in a variable and using \"" for " and %% for % are what allows it to work.

If you prefer a one-liner (again, from batch) you can use:

REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*" /f

If you want the same functionality, don't forget to set the verb for VBSFile to Open2 so you can just double-click VBS files to run them with CScript:

REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell" /ve /t REG_SZ /d "Open2" /f
0

精彩评论

暂无评论...
验证码 换一张
取 消