reg - Work with the Registry
The reg command works with the Windows registry. It lets you view, add, change, and delete keys and values from Command Prompt or a batch file, just as you would in Registry Editor (regedit).
It is useful when you want to automate a settings change or apply the same setting to several computers.
Changing the registry incorrectly can stop Windows or applications from working properly. Always back up with reg export before you change or delete anything.
Registry Basics
The registry is a database that stores settings for Windows and applications. It consists of keys, which work like folders, and the values (name, type, and data) stored in them.
With the reg command, you specify a key with a path like this:
HKEY_CURRENT_USER\Software\MyApp
For the root key at the start of the path, you can use these abbreviations:
| Abbreviation | Full name | Contents |
|---|---|---|
HKLM | HKEY_LOCAL_MACHINE | Settings that apply to the whole computer |
HKCU | HKEY_CURRENT_USER | Settings for the current user only |
HKCR | HKEY_CLASSES_ROOT | File associations and similar settings |
HKU | HKEY_USERS | Settings for all users |
HKCC | HKEY_CURRENT_CONFIG | The current hardware configuration |
HKCU holds the current user’s settings, so you can change it with normal privileges. Writing to HKLM, however, requires administrator rights. Without them, you get “Access is denied.” See the related articles for details.
Basic Syntax of the reg Command
reg <operation> <key name> [options]
The available operations are listed below. This article focuses on the commonly used query, add, delete, export, and import.
| Operation | Description |
|---|---|
query | Display keys and values |
add | Add keys and values (existing values are overwritten after confirmation) |
delete | Delete keys and values |
export | Write a key to a .reg file |
import | Load the contents of a .reg file into the registry |
copy | Copy a key to another location |
compare | Compare two keys or values |
save / restore | Save and restore a key as a binary file |
load / unload | Load a saved hive into the registry, or unload it |
If a key name or value name contains spaces, enclose it in double quotation marks ("), as in "HKCU\Software\My App".
Display Keys and Values (reg query)
Display the contents of a key
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
This lists the values and subkeys directly under the specified key. This key holds the apps that start automatically when you sign in.
Each line shows the value name, the value type, and the data, in that order.
Display only a specific value
Use /v to specify the value name.
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
To display the default value, which has no name, use /ve instead of /v.
Display or search including subkeys
Add /s to display everything, including subkeys. Combine it with /f to search for text.
reg query "HKCU\Software\MyApp" /s
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /s /f Chrome
query options
| Option | Description |
|---|---|
/v name | Display only the value with the specified name |
/ve | Display the default value (no name) |
/s | Display recursively, including subkeys |
/f text | Text to search for (partial match, case-insensitive by default) |
/k | Limit the /f search to key names |
/d | Limit the /f search to data |
/c | Make the search case-sensitive |
/e | Match only exact results |
/t type | Display only values of the specified type |
Value Types
Every value has a type, which you specify with /t in reg add.
| Type | Description |
|---|---|
REG_SZ | A string (this is the default if you omit the type) |
REG_EXPAND_SZ | A string that can contain environment variables such as %PATH% |
REG_MULTI_SZ | Multiple strings (separated by \0 by default) |
REG_DWORD | A 32-bit number, often used for on/off settings (0 and 1) |
REG_QWORD | A 64-bit number |
REG_BINARY | Binary data |
REG_NONE | No type |
Add or Change Keys and Values (reg add)
reg add <key name> [/v name | /ve] [/t type] [/d data] [/f]
If the key does not exist, it is created along with any intermediate keys. If the specified value already exists, it is overwritten after confirmation (add /f to overwrite without confirmation).
reg add "HKCU\Software\MyApp" /v UserName /t REG_SZ /d "taro"
reg add "HKCU\Software\MyApp" /v Enabled /t REG_DWORD /d 1
When it succeeds, the message “The operation completed successfully.” appears.
add options
| Option | Description |
|---|---|
/v name | Name of the value to add |
/ve | Set the default value (no name) |
/t type | Value type (REG_SZ if omitted) |
/d data | Data to set (for REG_DWORD, a decimal number or a hexadecimal number starting with 0x) |
/s separator | One character that separates multiple strings in REG_MULTI_SZ (default \0) |
/f | Overwrite an existing value without confirmation |
If you run reg add on a value that already exists, a confirmation message (Yes/No) asks whether to overwrite it, and the command waits for input. In a batch file that runs automatically, the script stops unless you add /f.
Example: Show file extensions
The setting that shows file extensions in File Explorer is stored in a value named HideFileExt. Setting it to 0 shows extensions.
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v HideFileExt /t REG_DWORD /d 0 /f
The setting takes effect after you reopen File Explorer or sign in again.
Delete Keys and Values (reg delete)
reg delete "HKCU\Software\MyApp" /v UserName /f
reg delete "HKCU\Software\MyApp" /f
| Option | Description |
|---|---|
/v name | Delete only the specified value |
/ve | Delete the default value (no name) |
/va | Delete all values in the key (subkeys remain) |
/f | Delete without confirmation |
If you run reg delete without any of /v, /ve, or /va, the key and all of its subkeys and values are deleted. With /f, no confirmation appears and you cannot undo it. Check the key name and whether /v is present before you run it.
Back Up and Restore (reg export / reg import)
Before you change the registry, write the target key to a .reg file.
reg export "HKCU\Software\MyApp" "%USERPROFILE%\Desktop\myapp-backup.reg"
If a file with the same name exists, you are asked whether to overwrite it. Add /y to overwrite without confirmation.
reg import "%USERPROFILE%\Desktop\myapp-backup.reg"
Double-clicking an exported .reg file also imports its contents into the registry. Note that reg import only adds or overwrites the keys and values written in the file. It does not delete values that were added after the backup. To return completely to the original state, delete the target key with reg delete first, and then import.
Using It in Batch Files
Read a value into a variable
The output of reg query has the form value name type data. You can split it on whitespace with for /f and extract the data.
@echo off
set KEY=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
for /f "tokens=2*" %%a in ('reg query "%KEY%" /v ProductName ^| find "ProductName"') do set PRODUCT=%%b
echo Product name: %PRODUCT%
tokens=2* assigns the second item (the type) to %%a and everything after it (the data) to %%b. Even if the data contains spaces, %%b captures it all. However, if the value name itself contains spaces, the item positions shift and this method does not work.
Check whether a key or value exists
reg query returns exit code 1 if the target does not exist. You can use this to check for existence.
@echo off
reg query "HKCU\Software\MyApp" /v Enabled >nul 2>&1
if errorlevel 1 (
echo No setting found. Registering the default.
reg add "HKCU\Software\MyApp" /v Enabled /t REG_DWORD /d 1 /f >nul
) else (
echo Setting found.
)
>nul 2>&1 suppresses both standard output and error output.
32-bit and 64-bit Registry Views
On 64-bit Windows, some keys such as HKLM\SOFTWARE are split so that 32-bit apps see a separate location (WOW6432Node). The reg command has options to choose which view to operate on.
| Option | Description |
|---|---|
/reg:32 | Operate on the 32-bit registry view |
/reg:64 | Operate on the 64-bit registry view |
reg query "HKLM\SOFTWARE\MyApp" /reg:32
You may need to specify this when working with settings that a 32-bit app reads and writes.
Summary
| What you want to do | Command |
|---|---|
| Display the contents of a key | reg query "key name" |
| Display a specific value | reg query "key name" /v value name |
| Search by text | reg query "key name" /s /f search text |
| Add or change a value | reg add "key name" /v name /t type /d data /f |
| Delete a value | reg delete "key name" /v name /f |
| Delete a key | reg delete "key name" /f |
| Make a backup | reg export "key name" filename.reg |
| Restore from a backup | reg import filename.reg |