TExcelTAD
Home › Windows CMD & Batch › reg: The Registry

reg: The Registry

The registry is Windows' central configuration database: installed programs, file associations, driver and service settings, user preferences, the Windows version itself. Graphical tools change it behind the scenes; regedit lets you browse it; and reg.exe lets you read and write it from Command Prompt and batch files. How it is stored on disk (hive files, NTUSER.DAT) is explained in The Registry in the OS course.

Keys, values and types

The registry looks like a file system: keys are folders, values are the files inside them, and every value has a name, a type and data.

text
HKEY_CURRENT_USER                          <- root key (hive)
 └── Software
      └── TadDemo                          <- key
           ├── (Default)   REG_SZ     (value not set)
           ├── Theme       REG_SZ     dark          <- value: name, type, data
           ├── FontSize    REG_DWORD  0xe
           └── Recent      REG_MULTI_SZ  a.txt\0b.txt

The five root keys, with the short names reg accepts:

Short Full name Contains
HKLM HKEY_LOCAL_MACHINE machine-wide settings: software, services, drivers. Writing needs admin rights
HKCU HKEY_CURRENT_USER the logged-in user's settings (loaded from their NTUSER.DAT)
HKU HKEY_USERS all loaded user profiles; HKCU is one of them
HKCR HKEY_CLASSES_ROOT file associations and COM classes (a merged view of HKLM and HKCU Software\Classes)
HKCC HKEY_CURRENT_CONFIG current hardware profile

The value types you'll meet:

Type Holds Example
REG_SZ a string dark
REG_EXPAND_SZ a string with %VARIABLES% expanded when read %USERPROFILE%\Tools
REG_DWORD a 32-bit number, shown in hex 0x1
REG_QWORD a 64-bit number 0x2540be400
REG_MULTI_SZ a list of strings a.txt\0b.txt
REG_BINARY raw bytes 01A0FF

reg query: reading

cmd
C:\Users\ada>reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v DisplayVersion

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    DisplayVersion    REG_SZ    24H2


C:\Users\ada>reg query "HKCU\Control Panel\Desktop" /v WallPaper

HKEY_CURRENT_USER\Control Panel\Desktop
    WallPaper    REG_SZ    C:\Users\ada\Pictures\lake.jpg


C:\Users\ada>reg query HKCU\Software\NoSuchApp
ERROR: The system was unable to find the specified registry key or value.

Sample output. Each value is printed as name, type, data, separated by four spaces. Quote paths that contain spaces. The main options:

Option Effect
/v Name one value (/ve = the (Default) value)
(no /v) all values of the key, then its subkeys
/s recurse into all subkeys
/f text search for text in key names, value names and data (combine with /s; /k keys only, /d data only, /e exact)
/t REG_SZ only values of this type

reg query exits with 0 when it found something and 1 when it didn't, so reg query ... >nul 2>&1 && echo found is the standard "does this key exist?" test.

Parsing reg query output

The playground has no registry, so the following examples save reg query output in a file and parse it. On a real PC you'd write for /f ... in ('reg query "..." /v Name 2^>nul') instead.

Value lines start with spaces and have three parts. tokens=1,2,* gives the name, the type and all the rest as the data, so data with spaces stays whole. Running the text through findstr /c:"REG_" first keeps only the value lines and drops the blank line and the key path:

cmd · try it yourself
@echo off
(
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
echo     ProductName    REG_SZ    Windows 10 Pro
echo     DisplayVersion    REG_SZ    24H2
echo     CurrentBuild    REG_SZ    26100
echo     UBR    REG_DWORD    0x19b8
echo     EditionID    REG_SZ    Professional
) > winver.txt

for /f "tokens=1,2,*" %%a in ('findstr /c:"REG_" winver.txt') do (
  echo name=%%a  type=%%b  data=[%%c]
  set "reg_%%a=%%c"
)
set /a ubr=%reg_UBR%
echo.
echo Windows %reg_DisplayVersion%, build %reg_CurrentBuild%.%ubr% (%reg_EditionID%)
name=ProductName  type=REG_SZ  data=[Windows 10 Pro]
name=DisplayVersion  type=REG_SZ  data=[24H2]
name=CurrentBuild  type=REG_SZ  data=[26100]
name=UBR  type=REG_DWORD  data=[0x19b8]
name=EditionID  type=REG_SZ  data=[Professional]

Windows 24H2, build 26100.6584 (Professional)

Two lessons hide in this output:

  • REG_DWORD data is printed in hex. set /a understands the 0x prefix, so set /a ubr=0x19b8 gives 6584, the update revision that ver shows.
  • Yes, ProductName really says "Windows 10 Pro" on Windows 11 machines; Microsoft never updated it. Trust CurrentBuild (22000 or higher = Windows 11), as in System Information.

Listing installed programs

Every installed program registers an uninstall entry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall (64-bit), HKLM\SOFTWARE\WOW6432Node\...\Uninstall (32-bit) and HKCU\...\Uninstall (per-user installs). reg query <key> /s /v DisplayName prints one block per program. Keep only the value lines and you have a software inventory:

cmd · try it yourself
@echo off
setlocal enabledelayedexpansion
(
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1
echo     DisplayName    REG_SZ    Git
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4F3A2C1B-7E2D-4B8A-9C61-2D7E5A1F0B93}
echo     DisplayName    REG_SZ    Python 3.13.1 Core Interpreter
echo.
echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip
echo     DisplayName    REG_SZ    7-Zip 24.09
echo.
echo End of search: 3 match^(es^) found.
) > uninstall.txt

set /a n=0
for /f "tokens=2,*" %%a in ('findstr /c:"REG_SZ" uninstall.txt') do (
  set /a n+=1
  echo !n!. %%b
)
findstr /i /c:"python 3." uninstall.txt >nul && echo Python is installed
findstr /i /c:"node.js" uninstall.txt >nul || echo Node.js is NOT installed
1. Git
2. Python 3.13.1 Core Interpreter
3. 7-Zip 24.09
Python is installed
Node.js is NOT installed

Here tokens=2,* skips the value name: %%a is the type and %%b the full data.

reg add, reg delete: writing

Your own settings belong under HKCU\Software\<YourApp>, which every user may write without elevation:

cmd
C:\Users\ada>reg add HKCU\Software\TadDemo /v Theme /t REG_SZ /d dark /f
The operation completed successfully.

C:\Users\ada>reg add HKCU\Software\TadDemo /v FontSize /t REG_DWORD /d 14 /f
The operation completed successfully.

C:\Users\ada>reg query HKCU\Software\TadDemo

HKEY_CURRENT_USER\Software\TadDemo
    Theme    REG_SZ    dark
    FontSize    REG_DWORD    0xe


C:\Users\ada>reg delete HKCU\Software\TadDemo /v Theme /f
The operation completed successfully.

C:\Users\ada>reg delete HKCU\Software\TadDemo /f
The operation completed successfully.

Sample session. reg add creates missing keys on the way; /t defaults to REG_SZ; /d 14 with REG_DWORD is decimal input, shown back as 0xe. /f skips the "Overwrite / Delete (Yes/No)?" question, which a script needs, but it also removes your safety net: reg delete <key> /f deletes the whole key with all subkeys, without asking. Writing under HKLM from a normal prompt fails with ERROR: Access is denied.

reg export and import: backups

Before changing anything, back it up. reg export writes a .reg text file; reg import (or double-clicking the file) merges it back:

cmd
C:\Users\ada>reg export HKCU\Software\TadDemo tad-backup.reg /y
The operation completed successfully.

C:\Users\ada>type tad-backup.reg
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\TadDemo]
"Theme"="dark"
"FontSize"=dword:0000000e

C:\Users\ada>reg import tad-backup.reg
The operation completed successfully.

Sample session. A .reg file is plain text, so you can also write one from a batch file. Note how backslashes are doubled inside strings and DWORDs are 8 hex digits:

cmd · try it yourself
@echo off
set "file=settings.reg"
> "%file%" echo Windows Registry Editor Version 5.00
>>"%file%" echo.
>>"%file%" echo [HKEY_CURRENT_USER\Software\TadDemo]
>>"%file%" echo "Theme"="dark"
>>"%file%" echo "InstallDir"="C:\\Tools\\TadDemo"
>>"%file%" echo "FontSize"=dword:0000000e
>>"%file%" echo "OldSetting"=-
type "%file%"
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\TadDemo]
"Theme"="dark"
"InstallDir"="C:\\Tools\\TadDemo"
"FontSize"=dword:0000000e
"OldSetting"=-

"OldSetting"=- deletes that value on import, and [-HKEY_...] with a minus deletes a whole key. reg import settings.reg would apply it (regedit /s settings.reg does the same silently). Other sub-commands: reg copy (copy a key), reg compare (diff two keys), reg save/reg load (binary hive files, admin only).

Warning: a wrong registry change can stop programs or Windows itself from starting. Export first, change one thing at a time, and prefer HKCU over HKLM whenever the setting exists in both.

PowerShell equivalents

PowerShell exposes the registry as drives (HKLM:, HKCU:), so the file commands work on it:

CMD PowerShell
reg query "HKLM\...\CurrentVersion" /v CurrentBuild Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name CurrentBuild
reg query HKCU\Software\TadDemo Get-ItemProperty HKCU:\Software\TadDemo
reg add HKCU\Software\TadDemo /f New-Item HKCU:\Software\TadDemo -Force
reg add ... /v Theme /d dark /f Set-ItemProperty HKCU:\Software\TadDemo -Name Theme -Value dark
reg delete ... /v Theme /f Remove-ItemProperty HKCU:\Software\TadDemo -Name Theme
reg delete HKCU\Software\TadDemo /f Remove-Item HKCU:\Software\TadDemo -Recurse
powershell
PS C:\Users\ada> (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').UBR
6584

Sample output: DWORDs come back as numbers, no hex conversion needed. More in Providers & Drives.

Test yourself

Which root key holds settings for the currently logged-in user only?

  1. HKLM
  2. HKCU
  3. HKCR
  4. HKCC

Why does a batch file parse reg query value lines with tokens=1,2,*?

  1. Because reg query prints exactly three words
  2. The * puts all remaining text in the third variable, so data containing spaces is kept whole
  3. It sorts the values by type
  4. It converts hex numbers to decimal

What does /f do in reg delete HKCU\Software\TadDemo /f?

  1. Deletes only the values, keeping subkeys
  2. Forces the deletion without asking for confirmation
  3. Creates a backup file first
  4. Filters which values are deleted