Batch Script - Adding to the Registry


Advertisements

Adding to the registry is done via the REG ADD command. Note that in order to add values to the registry you need to have sufficient privileges on the system to perform this operation.

Syntax

The REG ADD command has the following variations. In the second variation, no name is specified for the key and it will add the name of “(Default)” for the key.

REG ADD [ROOT\]RegKey /v ValueName [/t DataType] [/S Separator] [/d Data] [/f]
REG ADD [ROOT\]RegKey /ve [/d Data] [/f]

Where

  • ValueName − The value, under the selected RegKey, to edit.

  • /d Data − The actual data to store as a "String", integer, etc.

  • /f − Force an update without prompting "Value exists, overwrite Y/N".

  • /S Separator − Character to use as the separator in REG_MULTI_SZ values. The default is "\0".

  • /t DataType − These are the data types defined as per the registry standards which can be −

    • REG_SZ (default)

    • REG_DWORD

    • REG_EXPAND_SZ

    • REG_MULTI_SZ

Example

@echo off 
REG ADD HKEY_CURRENT_USER\Console /v Test /d "Test Data" 
REG QUERY HKEY_CURRENT_USER\Console /v Test

In the above example, the first part is to add a key into the registry under the location HKEY_CURRENT_USER\Console. This key will have a name of Test and the value assigned to the key will be Test Data which will be of the default string type.

The second command just displays what was added to the registry by using the REG QUERY command.

Output

Following will be the output of the above program. The first line of the output shows that the ‘Add’ functionality was successful and the second output shows the inserted value into the registry.

The operation completed successfully. 
HKEY_CURRENT_USER\Console 
   Test REG_SZ Test Data
batch_script_registry.htm
Advertisements