Skip to main content

How to Iterate Through All Values Under a Registry Key in Batch Script

In advanced scripting for system administration or application diagnostics, you often need to read all the settings stored within a specific registry key. You might not know the names of all the values beforehand, so you need a way to discover and loop through them. While there is no direct FOR-EACH-VALUE command, this is a classic task that can be accomplished by parsing the output of the built-in REG.EXE utility.

This guide will teach you how to use a FOR /F loop to process the output of the REG QUERY command. You will learn how to correctly parse the command's output to extract the name, type, and data for each value under a key, creating a powerful tool for registry inventory and analysis.

The Challenge: No Native Command to Iterate Values

The cmd.exe interpreter does not have a looping structure designed for the registry. You cannot do FOR %%V IN HKCU\Software\.... Instead, you must use a command that can list the contents of a key as text (REG QUERY), and then use a second command (FOR /F) to parse that text output line by line.

The Core Command: REG QUERY

The REG QUERY command is the standard tool for displaying the contents of a registry key.

Syntax: REG QUERY "<KeyName>"

The output is formatted for human reading, with a header, a blank line, and then a list of values.

HKEY_CURRENT_USER\Control Panel\Desktop

Wallpaper REG_SZ C:\Path\To\image.jpg
TileWallpaper REG_SZ 0
WallpaperStyle REG_SZ 10

Our script's job is to parse this text to extract the three columns of information for each value.

The Complete Script to List All Values

This script will iterate through all the values under the specified key and print their name, type, and data.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "TargetKey=HKCU\Control Panel\Desktop"

ECHO --- Listing all values under the key ---
ECHO [%TargetKey%]
ECHO.

REM 'skip=2' ignores the key path header and the blank line.
REM 'tokens=1,2,*' grabs the name, type, and the rest of the line (the data).
FOR /F "skip=2 tokens=1,2,*" %%A IN ('REG QUERY "%TargetKey%"') DO (

REM Optional: Skip the (Default) value if you only want named values.
IF /I "%%A" NEQ "(Default)" (
SET "ValueName=%%A"
SET "ValueType=%%B"
SET "ValueData=%%C"

ECHO Name: !ValueName!
ECHO Type: !ValueType!
ECHO Data: !ValueData!
ECHO --------------------
)
)

ENDLOCAL

How the Script Works (A Step-by-Step Breakdown)

  • SETLOCAL ENABLEDELAYEDEXPANSION: This is important for correctly handling the variables inside the loop, especially if the data contains special characters.
  • FOR /F "skip=2 tokens=1,2,*" %%A IN ('REG QUERY ...'): This is the core of the script.
    • 'REG QUERY "%TargetKey%"': This command is executed first, and its text output is captured.
    • skip=2: This tells the FOR loop to ignore the first two lines of the output (the line with the key path and the following blank line).
    • tokens=1,2,*: This is the parsing logic. The default delimiter is a space.
      • 1: The first block of text (the Value Name) is assigned to %%A.
      • 2: The second block of text (the Value Type, e.g., REG_SZ) is assigned to %%B.
      • *: A special token that tells the loop to assign everything else on the line to the next variable, %%C. This is crucial because it correctly captures data that contains spaces.

Common Pitfalls and How to Solve Them

Problem: "Access is denied." (Administrator Privileges)

If you are trying to query a key in a protected location like HKEY_LOCAL_MACHINE (HKLM), you will need elevated rights.

Solution: The script must be run as an Administrator.

Problem: Values with Spaces in Their Names

This is the most significant limitation of this pure-batch method. The tokens=1,2,* logic relies on the Value Name being a single word without spaces. If a value is named "My App Setting", the FOR /F loop will incorrectly assign "My" to %%A, "App" to %%B, and "Setting" to %%C.

Solution: There is no simple, reliable "pure-batch" solution for this edge case. For keys that contain values with spaces in their names, you must use a more powerful scripting language like PowerShell, which has a true registry provider and can handle this correctly.

Problem: Handling the "(Default)" Value

Every key has an unnamed "(Default)" value. The REG QUERY command lists this, which you may not want.

Solution: The script provided in section before already demonstrates the fix. An IF statement inside the loop checks for and skips this specific entry. IF /I "%%A" NEQ "(Default)" ( ... )

Practical Example: Listing All User Startup Programs

This is a perfect real-world use case. The script queries the standard "Run" key for the current user to see which programs are configured to start automatically on logon. This key is safe to read without administrator rights.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "RunKey=HKCU\Software\Microsoft\Windows\CurrentVersion\Run"

ECHO --- Current User Startup Programs ---
ECHO Reading from: %RunKey%
ECHO.

FOR /F "skip=2 tokens=1,*" %%A IN ('REG QUERY "%RunKey%"') DO (
SET "StartupName=%%A"
SET "CommandPath=%%B"

REM The command path often has REG_SZ as the first token, which we can ignore.
REM We need to parse %%B to get the real value.
FOR /F "tokens=1,*" %%X IN ("!CommandPath!") DO (
IF /I "%%X"=="REG_SZ" (
ECHO Program: !StartupName!
ECHO Command: %%Y
ECHO.
)
)
)

ENDLOCAL

Conclusion

While batch scripting has no direct way to loop through registry values, it is a task that is easily accomplished by parsing the output of the REG QUERY command with a FOR /F loop.

Key takeaways for a successful script:

  • Use REG QUERY "KeyName" to get the list of values as text.
  • Use a FOR /F "skip=2 tokens=1,2,*" loop to parse this text and capture the name, type, and data.
  • Be aware of the limitation with value names that contain spaces; for those, PowerShell is the better tool.
  • Use an IF statement inside the loop to optionally filter out the (Default) value.