39

How do I modify an existing registry key value from cmd?

There are a lot of options and switches, bells and whistles, for the Reg command. There are options to query, add, and delete keys, subkeys, and value names. But how do you modify the data of an existing value name? Do you completely remove the value name, and then add it again and re-type the modified value/data? That seems very cumbersome...

I am not interested in any script/batch automation, at least not now, I just want plain simple registry editing through cmd Reg command. Without the need to destroy a key just to add it a second later with some additional data. The same kind of operation you can do in Regedit.

Here's an example for you to spur the thinking process.

Key: HKEY_CURRENT_USER\Software\Citrix\Receiver
Value type: reg_sz
Value name: UpgradeDone
Value data: true

Now this one is just fine, but what if I must change the value data from "true" to "false". So that I get this instead:

Key: HKEY_CURRENT_USER\Software\Citrix\Receiver
Value type: reg_sz
Value name: UpgradeDone
Value data: false

What are the options and syntax for doing that from cmd?....

Update:

I just realized now that the example above might have been just too easy...

In the above example I can just do following:

reg delete HKEY_CURRENT_USER\Software\Citrix\Receiver /v UpgradeDone

reg add HKEY_CURRENT_USER\Software\Citrix\Receiver /v UpgradeDone /t Reg_Sz /d false

I can afford completely deleting the value, and then adding it again, because the data contained within it is very small. It's just a string like "true" or "false". That's not too hard to re-create.

Here's a harder example with more data content:

Key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Value name: Path
Value type: Reg_Sz
Data: C:\Program Files (x86)\ImageMagick-6.8.5-Q16;C:\PROGRA~2\GTK2-R~1\bin;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Windows\System32\WindowsPowerShell\v1.0\;"C:\Program Files (x86)\WinHasher";C:\Program Files (x86)\Android\android-sdk\tools\;C:\Program Files (x86)\Android\android-sdk\platform-tools\;C:\Program Files (x86)\GTK2-Runtime\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files (x86)\Common Files\Teleca Shared;C:\Program Files (x86)\Notepad++

I want to remove only parts of the data content. Specifically, I only want to remove the last search path, the C:\Program Files (x86)\Notepad++.

How do you tackle this one??...

You don't expect me to re-type all this??......

1
  • Is your question specifically about editing PATH from the command line, or any value? Also, replacing or appending/prepending to any reg value is easy, but modifying/deleting parts of a value will require more effort.
    – Karan
    Jun 14, 2013 at 16:17

2 Answers 2

58

Use REG ADD with the "/f" parameter to force overwrite.

REG ADD /? explains the parameters.

REG ADD KeyName [/v ValueName | /ve] [/t type] [/s Separator] [/d Data] [/f]

KeyName     [\\Machine\]FullKey

Machine     Name of remote machine - omitting defaults to the current machine 
            Only HKLM and HKU are available on remote machines
FullKey     ROOTKEY\SubKey ROOTKEY [ HKLM | HKCU | HKCR | HKU | HKCC ] SubKey 
            The full name of a registry key under the selected ROOTKEY
/v          The value name, under the selected Key, to add
/ve         adds an empty value name <no name> for the key
/t          RegKey data types
            [ REG_SZ | REG_MULTI_SZ | REG_DWORD_BIG_ENDIAN | REG_DWORD | 
              REG_BINARY | REG_DWORD_LITTLE_ENDIAN | REG_NONE | REG_EXPAND_SZ ]
            If omitted, REG_SZ is assumed
/s          Specify one character that you use as the separator in your data 
            string for REG_MULTI_SZ. If omitted, use "\0" as the separator
/d          The data to assign to the registry ValueName being added
/f          Force overwriting the existing registry entry without prompt

To insert a new value or toggle a string value from "false" to "true", use a command like this:

reg add HKCU\Software\Citrix\Receiver /t REG_SZ /v UpgradeDone /d true /f

Example which reads an existing value and appends some string before writing it back:

set append=XXXXXX
set key=HKCU\Software\myTest
set value=myValue
set oldVal=

for /F "skip=2 tokens=3" %%r in ('reg query %key% /v %value%') do set oldVal=%%r
echo previous=%oldVal%

set newVal=%oldVal%%append% 

reg add %key% /v %value% /d %newVal% /f

Note: This sample assumes that the old value and the new value have no blanks. Otherwise one has to add quotes and change the "tokens" parameter. Extra error checking omitted for brevity. However, all registry manipulating code definitely must do adequate error checking.

7
  • 1
    So... forcing an "overwrite" actually appends data to the value name?... It doesn't overwrite the existing data??...
    – Samir
    Jun 14, 2013 at 9:30
  • So, using my second example above, you're saying that I can do reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v Path /d ;C:\Program Files (x86)\Some_software /f to append ;C:\Program Files (x86)\Some_software to the end of the string?
    – Samir
    Jun 14, 2013 at 10:00
  • No, "REG /ADD" just inserts or overwrites. To append, you'd have to "REG /QUERY" the value, compute the wanted value and "REG /ADD" it back. Jun 14, 2013 at 10:08
  • Yes, this seems to be the case. I just tried this. reg add HKEY_CURRENT_USER\Software\Citrix\Receiver /v UpgradeDone /t Reg_Sz /d false Value UpgradeDone exists, overwrite(Yes/No)? So without the /f switch it will ask you if you want to overwrite.
    – Samir
    Jun 14, 2013 at 10:22
  • Can you explain how to append data to an existing value name? What do you mean by "compute"?
    – Samir
    Jun 14, 2013 at 10:29
3

You can also REG import foo.reg in which foo.reg is a file containing the usual registry information as you'd export from regedit.exe.

In your example, this would be something like:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"Path"="C:\Program Files (x86)\ImageMagick-6.8.5-Q16;C:\PROGRA~2\GTK2-R~1\bin;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Windows\System32\WindowsPowerShell\v1.0\;"C:\Program Files (x86)\WinHasher";C:\Program Files (x86)\Android\android-sdk\tools\;C:\Program Files (x86)\Android\android-sdk\platform-tools\;C:\Program Files (x86)\GTK2-Runtime\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files (x86)\Common Files\Teleca Shared;C:\Program Files (x86)\Notepad++"

More information available at this Microsoft Technet article.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .