windows 在cmd(bat文件)中运行reg命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13809856/
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 10:07:36  来源:igfitidea点击:

Run reg command in cmd (bat file)?

windowsbatch-fileregistrycmd

提问by user198989

I'm trying to run this reg code in cmd (bat file), but I couldn't make it work. Where am I doing wrong?

我试图在 cmd(bat 文件)中运行这个 reg 代码,但我无法让它工作。我哪里做错了?

[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001

It works if I make it a reg file and double click.

如果我将其设为 reg 文件并双击,它就可以工作。

Bat file code (this doesn't work, no errors):

Bat 文件代码(这不起作用,没有错误):

@echo off
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /V HomePage /T REG_DWORD /F /D 1

回答by GolezTrol

You will probably get an UAC prompt when importing the reg file. If you accept that, you have more rights.

导入 reg 文件时,您可能会收到 UAC 提示。如果您接受这一点,您将拥有更多权利。

Since you are writing to the 'policies' key, you need to have elevated rights. This part of the registry protected, because it contains settings that are administered by your system administrator.

由于您正在写入“策略”键,因此您需要具有提升的权限。这部分注册表受到保护,因为它包含由系统管理员管理的设置。

Alternatively, you may try to run regedit.exefrom the command prompt.

或者,您可以尝试regedit.exe从命令提示符运行。

regedit.exe /S yourfile.reg

.. should silently import the reg file. See RegEdit Command Line Options Syntaxfor more command line options.

.. 应该默默地导入 reg 文件。有关更多命令行选项,请参阅RegEdit 命令行选项语法

回答by Noam Manos

In command line it's better to use REGtool rather than REGEDIT:

在命令行中最好使用REG工具而不是 REGEDIT:

REG IMPORT yourfile.reg

REG is designed for console mode, while REGEDIT is for graphical mode. This is why running regedit.exe /S yourfile.regis a bad idea, since you will not be notified if the there's an error, whereas REG Tool will prompt:

REG 设计用于控制台模式,而 REGEDIT 用于图形模式。这就是为什么运行regedit.exe /S yourfile.reg是个坏主意,因为如果出现错误,您将不会收到通知,而 REG 工具会提示:

>  REG IMPORT missing_file.reg

ERROR: Error opening the file. There may be a disk or file system error.

>  %windir%\System32\reg.exe /?

REG Operation [Parameter List]

  Operation  [ QUERY   | ADD    | DELETE  | COPY    |
               SAVE    | LOAD   | UNLOAD  | RESTORE |
               COMPARE | EXPORT | IMPORT  | FLAGS ]

Return Code: (Except for REG COMPARE)

  0 - Successful
  1 - Failed

For help on a specific operation type:

  REG Operation /?

Examples:

  REG QUERY /?
  REG ADD /?
  REG DELETE /?
  REG COPY /?
  REG SAVE /?
  REG RESTORE /?
  REG LOAD /?
  REG UNLOAD /?
  REG COMPARE /?
  REG EXPORT /?
  REG IMPORT /?
  REG FLAGS /?

回答by David Ruhmann

If memory serves correct, the reg addcommand will NOT create the entire directory path if it does not exist. Meaning that if any of the parent registry keys do not exist then they must be created manually one by one. It is really annoying, I know! Example:

如果内存正确,则该reg add命令将不会创建不存在的整个目录路径。这意味着如果任何父注册表项不存在,则必须逐一手动创建它们。这真的很烦人,我知道! 例子:

@echo off
reg add "HKCU\Software\Policies"
reg add "HKCU\Software\Policies\Microsoft"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v HomePage /t REG_DWORD /d 1 /f
pause

回答by Joe

You could also just create a Group Policy Preference and have it create the reg key for you. (no scripting involved)

您也可以只创建一个组策略首选项并让它为您创建 reg 密钥。(不涉及脚本)