如何设置 ExecutionPolicy:拒绝访问注册表项

atr*_*lla 23 powershell windows-server-2008

我正在以管理员身份运行 Windows Server 2008,我尝试将 ExecutionPolicy 设置为 PowerShell v2 的 Remotesigned,如下所示:

Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)

但我收到了这个错误:

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft
.PowerShell' is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  RemoteSigned
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma
   nd
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

小智 24

右键单击 Powershell 快捷方式并选择“以管理员身份运行”

  • 另请注意,如果您使用的是 64 位操作系统,则需要为 32 位和 64 位版本的 PowerShell 设置执行策略。 (5认同)

小智 15

Stephen Jennings 在StackOverflow 上回答了同样的问题

Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)

这将为当前用户(存储在 HKEY_CURRENT_USER 中)而不是本地机器(HKEY_LOCAL_MACHINE)设置执行策略。

  • 谢谢。这是正确的答案,不是每个人都拥有管理员权限,但这仍然有效。 (3认同)