从批处理脚本静默更改Powershell执行策略


4

我想要一些有关为Powershell设置注册表值的帮助。路径是

[hkey_local_machine \ system32 \ windows \ microsoft \ powershell \ 1 \ shellids \ microsoft.powershell]“路径” =“ c:\ windows \ system32 \ windowspowershell \ v1.0 \ powershell.exe”“ ExecutionPolicy” =“不受限制”

由于我是从一台好的机器导入的,因此当我运行.reg文件时,它可以正常工作。但是我希望在批处理文件中这样做。

当我手动调用命令提示符时,放入路径,例如c:\powershell.reg,这将导入值并根据需要覆盖注册表设置。
但是,如果l调用在批处理文件中执行相同的操作,则注册表中的值不会更改。在批处理文件中使用reg add命令,这些值将不起作用。


3
“不起作用”不足以说明正在发生的事情。
Daniel B

嗨,也许我应该提供一些信息。因此,从本质上讲,如果我手动调用命令提示符,放入路径例如c:\ powershell.reg,这将导入值并根据需要覆盖注册表设置。但是,如果l调用在批处理文件中执行相同的操作,则注册表中的值不会更改。因此批处理文件非常简单
Papoli 2015年

组策略不是一种选择吗?
Colyn1337

Answers:


10

为什么不简单地通过CMD运行以下命令

powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"

或直接在Powershell中使用(毕竟这就是命令的作用):

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force

1

如何从批处理脚本中静默更改Powershell执行策略?

警告:

下面的说明包含告诉您如何修改注册表的步骤。但是,如果您错误地修改了注册表,则可能会出现严重的问题。

因此,请确保您认真执行这些步骤。为了增加保护,请在修改注册表之前先对其进行备份。然后,如果出现问题,您可以还原注册表。

有关更多信息,请参见如何在Windows中备份和还原注册表。


reg

@echo off
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "Path" /d "c:\windows\system32\windowspowershell\v1.0\powershell.exe"
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "ExecutionPolicy" /d "unrestricted"

regedit

@echo off
regedit /s file.reg

其中file.reg包含以下内容:

[hkey_local_machine\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell] 
"Path"="c:\windows\system32\windowspowershell\v1.0\powershell.exe"
"ExecutionPolicy"="unrestricted"

注意:

  • [/s|-s]

    在命令行上指定文件名时,此开关用于禁止显示通常会显示的任何信息对话框。当应用程序的安装程序要使用.REG文件执行REGEDIT.EXE,但又不希望用户被显示的任何对话框所迷惑时,此功能很有用。


进一步阅读

  • Windows CMD命令行的AZ索引 -Windows cmd行相关的所有内容的出色参考。
  • reg-读取,设置或删除注册表项和值,然后从.REG文件保存和还原。
  • regedit-从文本(.REG)文件导入,导出或删除注册表设置。
  • regedit-命令行开关。

谢谢。我尝试了Regedit.exe / s命令,但失败了。我会再说一遍。谢谢
Papoli 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.