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

1

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.

snowcrash09

Posted 2012-07-23T16:41:01.797

Reputation: 111

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 – 2012-07-23T16:59:55.440

Can you give an example? I don't know how to parse the output in a batch file – snowcrash09 – 2012-07-24T13:14:56.337

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 – 2012-07-24T16:06:14.347

Can you use PowerShell instead? If so I could provide some sample code. – Chris N – 2012-08-01T02:31:14.133

Answers

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.

Josh C

Posted 2012-07-23T16:41:01.797

Reputation: 101