在命令提示符处添加、删除并测试注册表值*是否存在?

在命令提示符处添加、删除并测试注册表值*是否存在?

我正在尝试从命令提示符检查并更新注册表中的网络区域映射。我需要检查名为的值*。例如,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet 设置\ZoneMap\Domains\cmich.local,注册表编辑器显示以下内容:

* REG_DWORD 0x00000001 (1)

为了在命令脚本 (*.cmd) 中检查此值,我一直尝试使用 REG QUERY 命令。当我尝试通过传递*要匹配的值来执行此操作时,REG QUERY 命令会将星号视为通配符,只要该键存在,它就会返回匹配项。

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\example.com" /v "*" /reg:64

我尝试了多种转义字符组合(甚至那些我非常确定不会起作用的组合),包括以下所有带引号和不带引号的组合:,,,,,,。*这些都不起作用。\*^*`*`*`!*

我还需要添加和删除名为的值*,大概使用 REG ADD 和 REG DELETE。

*如何在命令提示符下添加、删除和测试具有名称的注册表值是否存在?

答案1

检查注册表值的一个棘手部分是命名*(实际上是一个含义通配符全部):

  • ?使用通配符仅查询单字符命名值,即最多一个字符, 和
  • 将结果缩小到*使用命名的值findstr公用事业
    • 事实上,reg query "%_TestKey%" /v ? | findstr /I /C:" * REG_"就足够了。

以下批处理文件代码片段显示添加、删除或更改名为 的值没有问题*

代码如下评论使用<NUL set /P =::: …技巧,即echo ::: …没有结尾CRLF,以最少的空行在输出中显示注释。

@ECHO ON
@SETLOCAL EnableExtensions DisableDelayedExpansion
@<NUL set /P =::: define a registry key
set "_TestKey=HKEY_CURRENT_USER\Software\Test Key"
@<NUL set /P =::: check all one-character named values 
reg query "%_TestKey%" /v ?
@<NUL set /P =::: narrow above result to the value named * using findstr utility 
reg query "%_TestKey%" /v ? | findstr /I /C:"%_TestKey%" /C:"    *    REG_"
@<NUL set /P =::: delete the value named *
reg delete "%_TestKey%" /v * /f
@<NUL set /P =::: add the value named *
reg add "%_TestKey%" /v * /t REG_DWORD /d 4 /f
@<NUL set /P =::: check the value named * 
reg query "%_TestKey%" /v ? | findstr /I /C:"%_TestKey%" /C:"    *    REG_"
@<NUL set /P =::: change the value named *
reg add "%_TestKey%" /v * /t REG_DWORD /d 1 /f
@<NUL set /P =::: check the value named * 
reg query "%_TestKey%" /v ? | findstr /I /C:"%_TestKey%" /C:"    *    REG_"

输出

==> D:\bat\SF\a867740.bat
::: define a registry key
==> set "_TestKey=HKEY_CURRENT_USER\Software\Test Key"
::: check all one-character named values
==> reg query "HKEY_CURRENT_USER\Software\Test Key" /v ?

HKEY_CURRENT_USER\Software\Test Key
    a    REG_SZ    Latin Small Letter A
    .    REG_SZ    Full Stop
    ?    REG_SZ    Question Mark
    *    REG_DWORD    0x1

End of search: 4 match(es) found.
::: narrow above result to the value named * using findstr utility
==> reg query "HKEY_CURRENT_USER\Software\Test Key" /v ?   | findstr /I /C:"HKEY_CURRENT_USER\Software\Test Key" /C:"    *    REG_"
HKEY_CURRENT_USER\Software\Test Key
    *    REG_DWORD    0x1
::: delete the value named *
==> reg delete "HKEY_CURRENT_USER\Software\Test Key" /v * /f
The operation completed successfully.
::: add the value named *
==> reg add "HKEY_CURRENT_USER\Software\Test Key" /v * /t REG_DWORD /d 4 /f
The operation completed successfully.
::: check the value named *
==> reg query "HKEY_CURRENT_USER\Software\Test Key" /v ?   | findstr /I /C:"HKEY_CURRENT_USER\Software\Test Key" /C:"    *    REG_"
HKEY_CURRENT_USER\Software\Test Key
    *    REG_DWORD    0x4
::: change the value named *
==> reg add "HKEY_CURRENT_USER\Software\Test Key" /v * /t REG_DWORD /d 1 /f
The operation completed successfully.
::: check the value named *
==> reg query "HKEY_CURRENT_USER\Software\Test Key" /v ?   | findstr /I /C:"HKEY_CURRENT_USER\Software\Test Key" /C:"    *    REG_"
HKEY_CURRENT_USER\Software\Test Key
    *    REG_DWORD    0x1

相关内容