windows 是否可以通过 .bat/.cmd 脚本修改注册表项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/130193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:22:50  来源:igfitidea点击:

Is it possible to modify a registry entry via a .bat/.cmd script?

windowsbatch-filecmdautomationregistry

提问by Brian R. Bondy

Is it possible to modify a registry value (whether string or DWORD) via a .bat/.cmd script?

是否可以通过 .bat/.cmd 脚本修改注册表值(无论是字符串还是 DWORD)?

采纳答案by Rui Vieira

You can use the REG command. From http://www.ss64.com/nt/reg.html:

您可以使用 REG 命令。来自http://www.ss64.com/nt/reg.html

Syntax:

   REG QUERY [ROOT\]RegKey /v ValueName [/s]
   REG QUERY [ROOT\]RegKey /ve  --This returns the (default) value

   REG ADD [ROOT\]RegKey /v ValueName [/t DataType] [/S Separator] [/d Data] [/f]
   REG ADD [ROOT\]RegKey /ve [/d Data] [/f]  -- Set the (default) value

   REG DELETE [ROOT\]RegKey /v ValueName [/f]
   REG DELETE [ROOT\]RegKey /ve [/f]  -- Remove the (default) value
   REG DELETE [ROOT\]RegKey /va [/f]  -- Delete all values under this key

   REG COPY  [\SourceMachine\][ROOT\]RegKey [\DestMachine\][ROOT\]RegKey

   REG EXPORT [ROOT\]RegKey FileName.reg
   REG IMPORT FileName.reg
   REG SAVE [ROOT\]RegKey FileName.hiv
   REG RESTORE \MachineName\[ROOT]\KeyName FileName.hiv

   REG LOAD FileName KeyName
   REG UNLOAD KeyName

   REG COMPARE [ROOT\]RegKey [ROOT\]RegKey [/v ValueName] [Output] [/s]
   REG COMPARE [ROOT\]RegKey [ROOT\]RegKey [/ve] [Output] [/s]

Key:
   ROOT :
         HKLM = HKey_Local_machine (default)
         HKCU = HKey_current_user
         HKU  = HKey_users
         HKCR = HKey_classes_root

   ValueName : The value, under the selected RegKey, to edit.
               (default is all keys and values)

   /d Data   : The actual data to store as a "String", integer etc

   /f        : Force an update without prompting "Value exists, overwrite Y/N"

   \Machine : Name of remote machine - omitting defaults to current machine.
                Only HKLM and HKU are available on remote machines.

   FileName  : The filename to save or restore a registry hive.

   KeyName   : A key name to load a hive file into. (Creating a new key)

   /S        : Query all subkeys and values.

   /S Separator : Character to use as the separator in REG_MULTI_SZ values
                  the default is "
reg add "HKCU\Software\etc\etc" /f /v "value" /t REG_SZ /d "Yes"
" /t DataType : REG_SZ (default) | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ Output : /od (only differences) /os (only matches) /oa (all) /on (no output)

回答by nray

@Franci Penov - modify ispossible in the sense of overwritewith /f, eg

@Franci佩诺夫-修改可能的意识覆盖/f,如

reg add HKCU\Software\SomeProduct
reg add HKCU\Software\SomeProduct /v Version /t REG_SZ /d v2.4.6

回答by Factor Mystic

Yes, you can script using the regcommand. Example:

是的,您可以使用该reg命令编写脚本。例子:

reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\etc\etc   /v Valuename /t REG_SZ /d valuedata  /f 

This would create key HKEY_CURRENT_USER\Software\SomeProduct, and add a String value "v2.4.6" named "Version" to that key.

这将创建 key HKEY_CURRENT_USER\Software\SomeProduct,并向该键添加名为“Version”的字符串值“v2.4.6”。

reg /?has the details.

reg /?有详细信息。

回答by Shersha Fn

This is how you can modify registry, without yes or no prompt and don't forget to run as administrator

这是您可以修改注册表的方法,没有是或否提示,并且不要忘记以管理员身份运行

reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice   /v ProgId /t REG_SZ /d IE.HTTPS  /f 

Below is a real example to set internet explorer as my default browser

下面是一个将 Internet Explorer 设置为我的默认浏览器的真实示例

##代码##

/f Force: Force an update without prompting "Value exists, overwrite Y/N"

/d Data : The actual data to store as a "String", integer etc

/v Value : The value name eg ProgId

/t DataType : REG_SZ (default) | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ

/f 强制:强制更新而不提示“值存在,覆盖 Y/N”

/d Data :要存储为“字符串”、整数等的实际数据

/v Value : 值名称,例如 ProgId

/t 数据类型:REG_SZ(默认)| REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ

Learn more about Read, Set or Delete registry keys and values, save and restore from a .REG file. from here

了解有关读取、设置或删除注册表项和值、从 .REG 文件保存和恢复的更多信息。从这里

回答by Lou Franco

You can make a .reg file and call start on it. You can export any part of the registry as a .reg file to see what the format is.

您可以制作一个 .reg 文件并对其调用 start 。您可以将注册表的任何部分导出为 .reg 文件以查看格式。

Format here:

格式在这里:

http://support.microsoft.com/kb/310516

http://support.microsoft.com/kb/310516

This can be run on any Windows machine without installing other software.

这可以在任何 Windows 机器上运行,而无需安装其他软件。

回答by Franci Penov

Yes. You can use reg.exe which comes with the OS to add, delete or query registry values. Reg.exe does not have an explicit modify command, but you can do it by doing delete and then add.

是的。您可以使用操作系统自带的 reg.exe 来添加、删除或查询注册表值。Reg.exe 没有明确的修改命令,但您可以通过先删除再添加来实现。

回答by Tim Jarvis

In addition to reg.exe, I highly recommend that you also check out powershell, its vastly more capable in its registry handling.

除了 reg.exe 之外,我强烈建议您还检查一下 powershell,它在注册表处理方面的能力要强得多。