52

Is it possible to modify a registry value (whether string or DWORD) via a .bat/.cmd script?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • As @Shersha Fn points out, you must be the Administrator to use REG.EXE. Is there a way to do this as a non-admin and avoid the 'Access is denied' message? – Mark Longmire Mar 15 '17 at 18:21

8 Answers8

114

@Franci Penov - modify is possible in the sense of overwrite with /f, eg

reg add "HKCU\Software\etc\etc" /f /v "value" /t REG_SZ /d "Yes"
Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
nray
  • 2,088
  • 4
  • 14
  • 11
  • 13
    +1 for the /f I've written couple of scripts for that but without /f it was such a pain; required me to be there when running the script to type "y+[return]" – amyassin Feb 16 '12 at 11:45
  • 12
    Thanks. This should be the accepted answer as it explains how to MODIFY an existing KEY. – Michael Butler Jun 17 '13 at 19:18
42

You can use the REG command. From http://www.ss64.com/nt/reg.html:

Syntax:

   REG QUERY [ROOT\]RegKey /v ValueName [/s]
   REG QUERY [ROOT\]RegKey /ve  --This returns the (default) value

   REG ADD [ROOT\]RegKey /v ValueName [/t DataType] [/S Separator] [/d Data] [/f]
   REG ADD [ROOT\]RegKey /ve [/d Data] [/f]  -- Set the (default) value

   REG DELETE [ROOT\]RegKey /v ValueName [/f]
   REG DELETE [ROOT\]RegKey /ve [/f]  -- Remove the (default) value
   REG DELETE [ROOT\]RegKey /va [/f]  -- Delete all values under this key

   REG COPY  [\\SourceMachine\][ROOT\]RegKey [\\DestMachine\][ROOT\]RegKey

   REG EXPORT [ROOT\]RegKey FileName.reg
   REG IMPORT FileName.reg
   REG SAVE [ROOT\]RegKey FileName.hiv
   REG RESTORE \\MachineName\[ROOT]\KeyName FileName.hiv

   REG LOAD FileName KeyName
   REG UNLOAD KeyName

   REG COMPARE [ROOT\]RegKey [ROOT\]RegKey [/v ValueName] [Output] [/s]
   REG COMPARE [ROOT\]RegKey [ROOT\]RegKey [/ve] [Output] [/s]

Key:
   ROOT :
         HKLM = HKey_Local_machine (default)
         HKCU = HKey_current_user
         HKU  = HKey_users
         HKCR = HKey_classes_root

   ValueName : The value, under the selected RegKey, to edit.
               (default is all keys and values)

   /d Data   : The actual data to store as a "String", integer etc

   /f        : Force an update without prompting "Value exists, overwrite Y/N"

   \\Machine : Name of remote machine - omitting defaults to current machine.
                Only HKLM and HKU are available on remote machines.

   FileName  : The filename to save or restore a registry hive.

   KeyName   : A key name to load a hive file into. (Creating a new key)

   /S        : Query all subkeys and values.

   /S Separator : Character to use as the separator in REG_MULTI_SZ values
                  the default is "\0" 

   /t DataType  : REG_SZ (default) | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ

   Output    : /od (only differences) /os (only matches) /oa (all) /on (no output)
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Rui Vieira
  • 5,253
  • 5
  • 42
  • 55
  • 22
    this answer is very vague and even though it lists all the possibilities (which is nice) it doesn't clearly answer the OP's question unlike @nray's answer – Dany Khalife Jun 06 '13 at 19:13
26

Yes, you can script using the reg command. Example:

reg add HKCU\Software\SomeProduct
reg add HKCU\Software\SomeProduct /v Version /t REG_SZ /d v2.4.6

This would create key HKEY_CURRENT_USER\Software\SomeProduct, and add a String value "v2.4.6" named "Version" to that key.

reg /? has the details.

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
14

This is how you can modify registry, without yes or no prompt and don't forget to run as administrator

reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\etc\etc   /v Valuename /t REG_SZ /d valuedata  /f 

Below is a real example to set internet explorer as my default browser

reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice   /v ProgId /t REG_SZ /d IE.HTTPS  /f 

/f Force: Force an update without prompting "Value exists, overwrite Y/N"

/d Data : The actual data to store as a "String", integer etc

/v Value : The value name eg ProgId

/t DataType : REG_SZ (default) | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ

Learn more about Read, Set or Delete registry keys and values, save and restore from a .REG file. from here

Shersha Fn
  • 1,511
  • 3
  • 26
  • 34
4

You can make a .reg file and call start on it. You can export any part of the registry as a .reg file to see what the format is.

Format here:

http://support.microsoft.com/kb/310516

This can be run on any Windows machine without installing other software.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
1

Yes. You can use reg.exe which comes with the OS to add, delete or query registry values. Reg.exe does not have an explicit modify command, but you can do it by doing delete and then add.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
1

In addition to reg.exe, I highly recommend that you also check out powershell, its vastly more capable in its registry handling.

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • PowerShell scripts must first be enabled on the client machine by using `Set-ExecutionPolicy`. I have a batch script which modifies the registry to set the execution policy to RemoteSigned. The benefit is that a user can enable and run PowerShell scripts without typing any commands. They can just double-click the batch file. – sourcenouveau Apr 12 '11 at 13:11
-2

See http://www.chaminade.org/MIS/Articles/RegistryEdit.htm

schaelle
  • 13
  • 2