The Windows Registry
The registry is Windows' central configuration database. Drivers, services, installed programs, file associations, user preferences and security settings are all stored in it. Where Linux spreads configuration over many text files in /etc and in your home directory, Windows keeps most of it in one tree of keys and typed values, managed by the kernel's Configuration Manager.
Keys, values and types
The registry looks like a file system: keys are folders, values are the data inside them. Each value has a name, a type and data. Every key also has an unnamed default value, shown as (Default).
HKEY_CURRENT_USER
+- Software
+- Microsoft
+- Notepad <- key
fWrap REG_DWORD 0x1 <- value: name, type, data
iPointSize REG_DWORD 0x78
lfFaceName REG_SZ Consolas| Type | Holds | Example |
|---|---|---|
REG_SZ |
a string | Consolas |
REG_EXPAND_SZ |
a string with %VARIABLES% expanded when read |
%SystemRoot%\system32\spoolsv.exe |
REG_MULTI_SZ |
a list of strings | RPCSS, http |
REG_DWORD |
a 32-bit number | 0x00000001 |
REG_QWORD |
a 64-bit number | 0x0000000100000000 |
REG_BINARY |
raw bytes | 01 00 ff |
Types matter: a program that reads a REG_DWORD and finds a REG_SZ usually ignores the setting. Most "registry tweak" mistakes are a wrong type, not a wrong value.
Root keys and hives
| Root key | Short | Contents |
|---|---|---|
HKEY_LOCAL_MACHINE |
HKLM |
machine-wide settings: hardware, drivers, services, installed software |
HKEY_CURRENT_USER |
HKCU |
settings of the signed-in user |
HKEY_USERS |
HKU |
all loaded user profiles (HKCU is a link to one of them) |
HKEY_CLASSES_ROOT |
HKCR |
file associations and COM classes: a merged view of HKLM\Software\Classes and HKCU\Software\Classes |
HKEY_CURRENT_CONFIG |
HKCC |
a link to the current hardware profile |
The data lives in hive files. A hive is a binary file that the Configuration Manager maps into a branch of the tree:
| Branch | Hive file |
|---|---|
HKLM\SYSTEM |
C:\Windows\System32\config\SYSTEM |
HKLM\SOFTWARE |
C:\Windows\System32\config\SOFTWARE |
HKLM\SAM |
C:\Windows\System32\config\SAM (local accounts, password hashes) |
HKLM\SECURITY |
C:\Windows\System32\config\SECURITY |
HKU\<your SID> (= HKCU) |
C:\Users\<you>\NTUSER.DAT |
HKU\<your SID>_Classes |
C:\Users\<you>\AppData\Local\Microsoft\Windows\UsrClass.dat |
HKLM\HARDWARE has no file at all: the kernel builds it at every boot. HKLM\SYSTEM\CurrentControlSet is a link to the active control set (ControlSet001), which holds services and drivers, the key winload.efi reads during boot.
HKLM vs HKCU
The rule is simple: machine-wide in HKLM, per-user in HKCU. Writing to HKLM needs administrator rights; every user can write to their own HKCU. Many programs read a setting from HKCU first and fall back to HKLM, so a user can override a machine default, and group policy writes to ...\Policies keys that override both.
A model of the registry
This Python model is a tiny registry: a tree of keys, typed values, case-insensitive paths, and the HKCR merge where the user's classes win over the machine's:
class Key:
def __init__(self):
self.subkeys = {} # lower-case name -> (display name, Key)
self.values = {} # lower-case name -> (display name, type, data)
def open(self, path, create=False):
key = self
for part in path.split("\\"):
low = part.lower() # the registry ignores case
if low not in key.subkeys:
if not create:
raise KeyError(f"key not found: {part}")
key.subkeys[low] = (part, Key())
key = key.subkeys[low][1]
return key
def set(self, name, rtype, data):
if rtype == "REG_DWORD" and not (isinstance(data, int) and 0 <= data < 2**32):
raise TypeError(f"{name}: REG_DWORD needs a 32-bit unsigned number")
if rtype == "REG_SZ" and not isinstance(data, str):
raise TypeError(f"{name}: REG_SZ needs a string")
self.values[name.lower()] = (name, rtype, data)
def get(self, name):
return self.values[name.lower()]
roots = {"HKLM": Key(), "HKCU": Key()}
def reg_add(path, name, rtype, data):
root, _, rest = path.partition("\\")
roots[root].open(rest, create=True).set(name, rtype, data)
def reg_query(path, name):
root, _, rest = path.partition("\\")
n, t, d = roots[root].open(rest).get(name)
shown = f"0x{d:x}" if t == "REG_DWORD" else d
return f"{n} {t} {shown}"
reg_add(r"HKCU\Software\Microsoft\Notepad", "fWrap", "REG_DWORD", 1)
reg_add(r"HKCU\Software\Microsoft\Notepad", "lfFaceName", "REG_SZ", "Consolas")
print(reg_query(r"HKCU\SOFTWARE\microsoft\NOTEPAD", "FWRAP"))
print(reg_query(r"HKCU\Software\Microsoft\Notepad", "lfFaceName"))
for bad in [("REG_DWORD", "1"), ("REG_DWORD", 2**32)]:
try:
reg_add(r"HKCU\Software\Tad", "Level", *bad)
except TypeError as e:
print("error:", e)
# HKCR = HKCU\Software\Classes layered over HKLM\Software\Classes
reg_add(r"HKLM\Software\Classes\.txt", "", "REG_SZ", "txtfile")
reg_add(r"HKLM\Software\Classes\.log", "", "REG_SZ", "txtfile")
reg_add(r"HKCU\Software\Classes\.txt", "", "REG_SZ", "VSCode.txt")
def hkcr(ext):
for root in ("HKCU", "HKLM"):
try:
return root, roots[root].open("Software\\Classes\\" + ext).get("")[2]
except KeyError:
pass
return None, None
for ext in [".txt", ".log", ".md"]:
print(f"HKCR\\{ext:<5} -> {hkcr(ext)}")fWrap REG_DWORD 0x1
lfFaceName REG_SZ Consolas
error: Level: REG_DWORD needs a 32-bit unsigned number
error: Level: REG_DWORD needs a 32-bit unsigned number
HKCR\.txt -> ('HKCU', 'VSCode.txt')
HKCR\.log -> ('HKLM', 'txtfile')
HKCR\.md -> (None, None)
Working with the registry from CMD
reg.exe reads and writes keys from any prompt (sample output — yours will differ):
C:\Users\ada>reg query HKCU\Software\Microsoft\Notepad /v fWrap
HKEY_CURRENT_USER\Software\Microsoft\Notepad
fWrap REG_DWORD 0x1
C:\Users\ada>reg add HKCU\Software\Tad /v Theme /t REG_SZ /d dark /f
The operation completed successfully.
C:\Users\ada>reg query HKCU\Software\Tad
HKEY_CURRENT_USER\Software\Tad
Theme REG_SZ dark
C:\Users\ada>reg export HKCU\Software\Tad tad.reg /y
The operation completed successfully.
C:\Users\ada>reg delete HKCU\Software\Tad /f
The operation completed successfully.The exported .reg file is plain text (UTF-16) that you can re-import by double-clicking it or with reg import:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Tad]
"Theme"="dark"Working with the registry from PowerShell
PowerShell exposes the registry as drives, HKLM: and HKCU:, so the usual item cmdlets work. Keys are items, values are item properties:
PS C:\Users\ada> New-Item -Path HKCU:\Software\Tad | Out-Null
PS C:\Users\ada> New-ItemProperty -Path HKCU:\Software\Tad -Name Level -PropertyType DWord -Value 3 | Out-Null
PS C:\Users\ada> Set-ItemProperty -Path HKCU:\Software\Tad -Name Theme -Value dark
PS C:\Users\ada> Get-ItemProperty -Path HKCU:\Software\Tad | Select-Object Level, Theme
Level Theme
----- -----
3 dark
PS C:\Users\ada> Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name CurrentBuild
26100
PS C:\Users\ada> Remove-Item -Path HKCU:\Software\Tad -RecurseTip:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstalllists installed programs, the same data Settings > Apps uses.
Important keys
| Key | What's there |
|---|---|
HKLM\SYSTEM\CurrentControlSet\Services |
every service and driver (Windows Services) |
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run |
programs started for every user at sign-in |
HKCU\Software\Microsoft\Windows\CurrentVersion\Run |
programs started for you at sign-in |
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment |
system environment variables |
HKCU\Environment |
your user environment variables |
HKLM\SOFTWARE\Policies, HKCU\Software\Policies |
group policy settings |
The Run keys are a favourite place for malware to persist; Task Manager's Startup apps page and Sysinternals Autoruns show them.
Registry vs Linux configuration files
| Windows registry | Linux | |
|---|---|---|
| Format | binary hives, typed values | text files (INI, YAML, shell syntax) |
| Machine settings | HKLM |
/etc |
| User settings | HKCU (NTUSER.DAT) |
dotfiles in ~, ~/.config |
| Desktop settings | HKCU\Control Panel, … |
dconf/GSettings (GNOME), KConfig files (KDE) |
| Transactions & locking | yes, handled by the kernel | per program |
Comments, diffs, git |
awkward (export to .reg) |
natural |
| Kernel parameters | HKLM\SYSTEM\...\Control |
/proc/sys, sysctl (/proc & /sys) |
Warning: Back up before editing:
reg exportthe key first, or create a restore point. A wrong value underHKLM\SYSTEMcan make Windows unbootable.
Learn more: Windows Services · Windows Users, UAC & ACLs · The Linux Directory Tree
Go further: Security-Relevant Registry Settings checks registry exports against a hardening baseline.
Test yourself
Which file stores HKEY_CURRENT_USER?
- C:\Windows\System32\config\SOFTWARE
- C:\Users\<you>\NTUSER.DAT
- C:\Windows\win.ini
- C:\Users\<you>\AppData\registry.db
What type should a 32-bit numeric setting such as fWrap have?
- REG_SZ
- REG_BINARY
- REG_DWORD
- REG_MULTI_SZ
What is HKEY_CLASSES_ROOT?
- A separate hive file
- A merged view of HKLM\Software\Classes and HKCU\Software\Classes where the user's entries win
- The list of user accounts
- Driver settings