許多網路文章將「執行指令」誤稱為「登錄檔命令」。真正的登錄檔操作指令是 reg.exe (命令列) 與 regedit.exe (匯入/匯出 .reg 檔)。本文專注於批次檔與 VBScript 實戰操作登錄檔的語法、參數詳解與常見範例。
Windows 內建,支援查詢、新增、修改、刪除、匯出、匯入、比較等操作,適合寫入批次檔自動化部署。
| 子命令 | 用途 |
|---|---|
query | 查詢鍵值或列舉子機碼 |
add | 新增機碼或鍵值 |
delete | 刪除機碼或鍵值 |
copy | 複製機碼 |
save | 將機碼存為 hive 檔 |
restore | 從 hive 檔還原 |
import / export | 匯入/匯出 .reg 檔 (等同 regedit /s) |
reg delete <機碼路徑> [/v 鍵值名稱] [/va] [/f]
/v ValueName:刪除指定鍵值 (不加則刪除整個機碼)。/va:刪除該機碼下所有鍵值,但保留機碼本身。/f:強制執行,不顯示確認提示 (批次檔必加)。% 字元在批次檔中寫 %%。reg delete "HKCU\Software\WinRAR\DialogEditHistory\ExtrPath" /va /f
reg delete "HKCU\Software\WinRAR\DialogEditHistory\ArcName" /va /f
reg delete "HKCU\Software\WinRAR\ArcHistory" /va /f
reg delete "HKCU\Software\CrocWare" /f
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify" /v PastIconsStream /f
reg delete "HKCU\Software\Alcohol Soft\Alcohol 120%%\Basic" /v "Image File Path" /f
reg delete "HKCU\Software\Alcohol Soft\Alcohol 120%%\MountedMRU" /f
:: 新增字串值
reg add "HKCU\Software\MyApp" /v "InstallPath" /t REG_SZ /d "C:\MyApp" /f
:: 新增 DWORD 值 (啟用某功能)
reg add "HKLM\SYSTEM\CurrentControlSet\Services\MyService" /v "Start" /t REG_DWORD /d 2 /f
:: 新增二進位值
reg add "HKCU\Control Panel\Desktop" /v "Wallpaper" /t REG_BINARY /d 00000000... /f
Dim oShell
Set oShell = CreateObject("WScript.Shell")
' 讀取值 (不存在會拋出錯誤,需 On Error Resume Next)
On Error Resume Next
val = oShell.RegRead("HKCU\Software\MyApp\Version")
If Err.Number = 0 Then WScript.Echo "Version: " & val
' 寫入字串值
oShell.RegWrite "HKCU\Software\MyApp\LastRun", Now, "REG_SZ"
' 寫入 DWORD
oShell.RegWrite "HKCU\Software\MyApp\Count", 1, "REG_DWORD"
' 刪除值
oShell.RegDelete "HKCU\Software\MyApp\TempValue"
' 刪除機碼 (需以 \ 結尾)
oShell.RegDelete "HKCU\Software\MyApp\"
regedit /e backup.reg "HKCU\Software\MyApp"regedit /s backup.reg (無提示視窗,適合登入腳本)Windows Registry Editor Version 5.00,機碼路徑用方括號,字串值雙引號,DWORD 用 dword:xxxxxxxx。regedit /e backup_YYYYMMDD.reg "HKLM\...\TargetKey")。reg query 驗證路徑存在。reg compare 對比兩台電腦或不同時刻的登錄檔差異。