Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use REG_EXPAND_SZ from the commandline?

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.

like image 565
colemik Avatar asked Dec 22 '22 23:12

colemik


2 Answers

From a command line, this worked for me:

reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot^%\system32
like image 123
aphoria Avatar answered Jan 12 '23 00:01

aphoria


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

like image 39
mivk Avatar answered Jan 12 '23 01:01

mivk