Skip to content

BAT script: Add, delete, modify and check registry entries through commands (take modifying registry entries to display the real-time seconds in the taskbar time in the lower right corner of Windows as an example)

Original link: https://www.itylq.com/bat-crud-registry.html

Release date: 2022-09-19 Migration time: 2026-03-21

The registry is a very important database in the Windows system, used to store system and application configuration information. By modifying certain registry key values, we can make the system or application start, run, display, etc. the way we want.

1 Registry key value query

REG QUERY KeyName [/v [ValueName] | /ve] [/s]
[/f Data [/k] [/d] [/c] [/e]] [/t Type] [/z] [/se Separator]
[/reg:32 | /reg:64]

KeyName: Registry key, used to describe and locate the absolute path of the registry key value, in the form of [\remote host name or IP]FullKey, such as "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" #Most registry operations are performed on the local computer. Remote operation of the registry requires relevant authorization on the target host.

FullKey: expressed in the form of RootKey\SubKey.

RootKey: can be called the registry root key, mainly [HKLM | HKCU | HKCR | HKU | HKCC], which correspond to HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_USERS and HKEY_CURRENT_CONFIG respectively;

SubKey: The full name of the registry key under the selected RootKey, that is, the relative path of the target key value except the root directory.

ValueName: Registry key value, the smallest operable unit in the registry, there are 7 main types, REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ, REG_DWORD, REG_QWORD, REG_BINARY, REG_NONE

2 Add/modify registry key value

REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [/d Data] [/f]
[/reg:32 | /reg:64]

/v: The name of the value to be added under the selected registry key; #For example, reg add “HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced” /v “AutoCheckSelect” /t REG_SZ /d 00000001 /f

/ve: Add a blank value name to the registry key (default default); #/ve parameter automatically creates a blank default value, see the "(default)" registry key value in the above figure, not followed by any string, such as reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /ve /t REG_DWORD /d 00000000 /f

/t: Registry key value type, there are 7 main types, see the definition in "ValueName"

/s: Specifies a character used as the delimiter in the REG_MULTI_SZ data string; if omitted, "\0" is used as the delimiter

/d: Data to be assigned to the added registry ValueName

/f: Forcefully overwrite existing registry keys without prompting

/reg:32 specifies registry keys that should be accessed using the 32-bit registry view

/reg:64 specifies registry keys that should be accessed using the 64-bit registry view

3 Delete registry key value

REG DELETE KeyName [/v ValueName | /ve | /va] [/f] [/reg:32 | /reg:64]

/va: Delete all values under this registry key

Attachment: Modify the registry key to display the real-time seconds in the taskbar in the lower right corner of Windows

@echo off
echo BAT script: display seconds in the taskbar time in the lower right corner of the windows system
echo -----------------------------------------
Echo execution process monitoring:
echo.
echo 1. Modifying registry keys...
::Query whether the ShowSecondsInSystemClock entry exists
reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "ShowSecondsInSystemClock"
::%errorlevel%Determine whether the registry key value exists based on the query resultsShowSecondsInSystemClock
if %errorlevel%==0 (
echo has been configured and does not need to be modified.
) else (
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "ShowSecondsInSystemClock" /t REG_DWORD /d 00000001
echo The registry modification is completed.
)
echo.
echo 2. Restart the resource manager...
taskkill /f /im explorer.exe & start explorer.exe
::Determine whether the resource manager restarts successfully
if %errorlevel%==0 (
echo The resource manager restart is complete.
) else (
echo Explorer failed to restart.
)
echo.
echo 3. The task execution is completed.
echo -----------------------------------------
pause

The taskbar displays real-time seconds:


This article was moved from WordPress to MkDocs