2

How can I append data to a Windows registry value in a batch file?

reg.exe seems to only support create/delete keys and values - I want to take an existing REG_MULTI_SZ value and append some more data to it.

4
  • Read the registry value into a variable (using "REG QUERY ...")and then modify the variable (append)... and then write it back to the registry.
    – Logman
    Jul 23, 2012 at 16:59
  • Can you give an example? I don't know how to parse the output in a batch file Jul 24, 2012 at 13:14
  • it would be easier if I knew what reg value you are attempting to modify, and what you are changing...is it hardcoded? are you passing a variable? Otherwise I could paste example code for each individual task but it would still have to be changed (bat code).
    – Logman
    Jul 24, 2012 at 16:06
  • Can you use PowerShell instead? If so I could provide some sample code.
    – Chris N
    Aug 1, 2012 at 2:31

1 Answer 1

0

If you have git and thus git bash then you could use a shell script instead. Here is a function that will append an argument to the Path registry key value.

function append_path(){
    tpath=$(reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" //v Path | grep -oP "%System.*")
    tpath="$tpath;$1"
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" //v Path //t REG_EXPAND_SZ //d "$tpath" //f
}

The double slashes may not be necessary, but in my experience I've found I need them for some commands to function correctly in a bash shell.

To use it, you would simply write

append_path C:/tools/bin

And of course if the path has spaces you'll need to provide quotations or the function will interpret anything following a space as a new argument. Of course you'll need to ensure the path is in a windows format. So if your script is acquiring the paths for you, you'll need to convert them. There are scripts written that do this, but that is for another SO question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.