Is it possible to modify a registry entry via a .bat/.cmd script?

WindowsBatch FileCmdAutomationRegistry

Windows Problem Overview


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

Windows Solutions


Solution 1 - Windows

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

Solution 2 - Windows

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)

Solution 3 - Windows

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.

Solution 4 - Windows

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

Solution 5 - Windows

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.

Solution 6 - Windows

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.

Solution 7 - Windows

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

Solution 8 - Windows

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian R. BondyView Question on Stackoverflow
Solution 1 - WindowsnrayView Answer on Stackoverflow
Solution 2 - WindowsRui VieiraView Answer on Stackoverflow
Solution 3 - WindowsFactor MysticView Answer on Stackoverflow
Solution 4 - WindowsShersha FnView Answer on Stackoverflow
Solution 5 - WindowsLou FrancoView Answer on Stackoverflow
Solution 6 - WindowsFranci PenovView Answer on Stackoverflow
Solution 7 - WindowsTim JarvisView Answer on Stackoverflow
Solution 8 - WindowsschaelleView Answer on Stackoverflow