简体   繁体   English

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

[英]Run reg command in cmd (bat file)?

I'm trying to run this reg code in cmd (bat file), but I couldn't make it work. 我正在尝试在cmd(bat文件)中运行此reg代码,但我无法使其工作。 Where am I doing wrong? 我哪里做错了?

[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

You will probably get an UAC prompt when importing the reg file. 导入reg文件时,您可能会收到UAC提示。 If you accept that, you have more rights. 如果您接受,那么您拥有更多权利。

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.exe from the command prompt. 或者,您可以尝试从命令提示符运行regedit.exe

regedit.exe /S yourfile.reg

.. should silently import the reg file. ..应该默默导入reg文件。 See RegEdit Command Line Options Syntax for more command line options. 有关更多命令行选项,请参阅RegEdit命令行选项语法

In command line it's better to use REG tool rather than REGEDIT: 在命令行中,最好使用REG工具而不是REGEDIT:

REG IMPORT yourfile.reg

REG is designed for console mode, while REGEDIT is for graphical mode. REG专为控制台模式而设计,而REGEDIT则用于图形模式。 This is why running regedit.exe /S yourfile.reg is a bad idea, since you will not be notified if the there's an error , whereas REG Tool will prompt: 这就是为什么运行regedit.exe / S yourfile.reg是一个坏主意,因为如果出现错误则不会收到通知 ,而REG Tool会提示:

>  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 /?

If memory serves correct, the reg add command will NOT create the entire directory path if it does not exist. 如果内存服务正确,则reg add命令将不会创建整个目录路径(如果它不存在)。 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: 例:

@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

You could also just create a Group Policy Preference and have it create the reg key for you. 您也可以创建组策略首选项并让它为您创建注册表项。 (no scripting involved) (不涉及脚本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM