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

185,970

Solution 1

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 2

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

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

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

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.

Share:
185,970
Brian R. Bondy
Author by

Brian R. Bondy

About me: Founder and lead developer for Brave Software, working on the Brave browser. Formerly a Khan Academy software engineer Mozillian (Mozilla contributor) C++, JavaScript, C, C#, Python, programming for > 20 years Graduate of the University of Waterloo; Married with twins boys, one singleton, and a red tri-colored border collie Past Microsoft MVP for Visual C++ Contributor to various other open source projects like WikiMedia Links: Blog/Personal website; Twitter: @brianbondy; Here is a picture of my pet unicorn:

Updated on May 30, 2020

Comments

  • Brian R. Bondy
    Brian R. Bondy almost 4 years

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

  • sourcenouveau
    sourcenouveau about 13 years
    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.
  • amyassin
    amyassin about 12 years
    +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]"
  • Dany Khalife
    Dany Khalife almost 11 years
    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
  • Michael Butler
    Michael Butler almost 11 years
    Thanks. This should be the accepted answer as it explains how to MODIFY an existing KEY.
  • nanonerd
    nanonerd over 4 years
    Thank you for the parameter definitions !