How to Work with Different Registry Value Types (DWORD, SZ, BINARY) in Batch Script
The Windows Registry doesn't just store information; it stores it in different data types. Understanding these types is crucial for correctly reading and, more importantly, writing registry values from a batch script. Setting a numerical value as a simple string, or vice versa, can cause an application to misbehave or fail entirely. The standard command-line tool for this, REG.EXE, gives you full control over the data types you create.
This guide will explain the most common registry value types and teach you how to use the REG ADD command with the correct syntax to create each one. You will learn the critical differences in data formatting, especially the non-obvious hexadecimal requirement for DWORD values.
The Most Common Registry Value Types Explained
While there are many types, you will work with these three most often in scripts.
| Type | Full Name | Description | Example Data |
|---|---|---|---|
REG_SZ | String | A simple, plain text string. Perfect for paths, names, or descriptions. | "Hello, World!" |
REG_DWORD | Double Word | A 32-bit number, often used as a boolean flag (0 for false, 1 for true) or for numerical settings. | 0x0000001a (26) |
REG_BINARY | Binary | Raw binary data, represented as a continuous sequence of hexadecimal characters. | ffeeddccbbaa |
REG_EXPAND_SZ | Expandable String | A string that can contain environment variables (like %windir%) which will be expanded by the application that reads it. | "%USERPROFILE%\Documents" |
The Core Command: REG ADD and its Type Switch (/t)
The REG ADD command is used to add or update registry values. To specify the type, you use the /t switch.
Syntax: REG ADD "<KeyName>" /v "<ValueName>" /t <DataType> /d <Data> /f
/v <ValueName>: The value's name./t <DataType>: The type of the value (e.g.,REG_SZ,REG_DWORD)./d <Data>: The data to be stored in the value. The format of this data depends on the type./f: Forces the overwrite without prompting. Essential for scripts.
How to Add a String Value (REG_SZ)
This is the most straightforward type. The data is simply a string of text.
@ECHO OFF
SET "Key=HKCU\Software\MyApp"
SET "ValueName=InstallPath"
SET "DataString=C:\Program Files\MyApp"
REG ADD "%Key%" /v "%ValueName%" /t REG_SZ /d "%DataString%" /f
How to Add a Number Value (REG_DWORD)
This is the most common point of confusion. The data for a REG_DWORD value must be specified as a hexadecimal number.
Let's set a value to the decimal number 26. The hexadecimal equivalent of 26 is 1a.
@ECHO OFF
SET "Key=HKCU\Software\MyApp"
SET "ValueName=MaxConnections"
SET "DataHex=0x1a"
REG ADD "%Key%" /v "%ValueName%" /t REG_DWORD /d %DataHex% /f
The 0x prefix is standard for hexadecimal numbers.
How to Add a Binary Value (REG_BINARY)
Binary data is provided as a continuous string of hexadecimal characters.
@ECHO OFF
SET "Key=HKCU\Software\MyApp"
SET "ValueName=UserPreferences"
SET "DataHex=0100ffab"
REG ADD "%Key%" /v "%ValueName%" /t REG_BINARY /d %DataHex% /f
How to Read the Values with REG QUERY
The REG QUERY command will correctly display the types of the values you've created, which is a great way to verify your script worked.
@ECHO OFF
SET "Key=HKCU\Software\MyApp"
ECHO --- Querying the registry key to see our values ---
REG QUERY "%Key%"
Output:
HKEY_CURRENT_USER\Software\MyApp
InstallPath REG_SZ C:\Program Files\MyApp
MaxConnections REG_DWORD 0x1a
UserPreferences REG_BINARY 0100ffab
Common Pitfalls and How to Solve Them
CRITICAL: The REG_DWORD Value Must Be Hexadecimal
If you try to provide a decimal number for a DWORD value, it will either fail or be misinterpreted.
Example of error:
REG ADD ... /t REG_DWORD /d 26- -> This will work, but it will store
0x26, which is decimal38, not26.
Solution: You must convert your decimal number to hexadecimal before using it with REG ADD. PowerShell is the easiest way to do this in a script.
@ECHO OFF
SET "DecimalValue=26"
FOR /F %%H IN ('powershell -Command "'{0:x}' -f %DecimalValue%"') DO SET "HexValue=0x%%H"
ECHO Decimal %DecimalValue% is %HexValue% in hex.
REG ADD ... /t REG_DWORD /d %HexValue% /f
Problem: "Access is denied." (Administrator Privileges)
If you are writing to a system-wide key in HKEY_LOCAL_MACHINE (HKLM), you will need elevated rights.
Solution: The script must be run as an Administrator.
Practical Example: Configuring Windows Explorer Settings
This script modifies two common Windows Explorer settings to demonstrate using REG_SZ and REG_DWORD in a real-world scenario.
@ECHO OFF
SETLOCAL
REM This script modifies HKCU, so it doesn't need admin rights.
SET "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
ECHO --- Configuring Windows Explorer ---
ECHO.
ECHO Setting 'Hidden' file visibility...
REM A DWORD value: 1 = Show hidden files, 2 = Don't show
REG ADD "%Key%" /v "Hidden" /t REG_DWORD /d 1 /f
ECHO.
ECHO Setting 'LaunchTo' to open to 'This PC'...
REM A SZ value: 1 = This PC, 2 = Quick access
REG ADD "%Key%" /v "LaunchTo" /t REG_DWORD /d 1 /f
ECHO.
ECHO [SUCCESS] Settings have been applied.
ECHO You may need to restart Explorer for changes to take effect.
ENDLOCAL
Conclusion
Understanding registry value types is essential for correctly and safely manipulating the registry from a batch script.
Key takeaways:
- Use the
/tswitch withREG ADDto specify the data type (REG_SZ,REG_DWORD, etc.). - Use the
/dswitch to provide the data for the value. - CRITICAL: The data for a
REG_DWORDmust be in hexadecimal format (e.g.,0x1a). REG_SZis for simple text, andREG_BINARYis for raw hexadecimal data.
By paying close attention to the data types and formatting, you can write powerful scripts that reliably configure applications and Windows settings.