65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Windows PowerShell 命令提示符下运行 PowerShell 脚本时出现“此系统上禁用脚本执行”错误

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.25/5 (3投票s)

2020年12月3日

CPOL

1分钟阅读

viewsIcon

6772

关于人们在首次运行任何 PowerShell 命令时遇到的“此系统上禁用脚本执行”错误

简单的解决方法

大多数人对更简单、更快速的解决方法感兴趣,所以在深入之前,我会直接告诉您解决方案。修复问题后,如果您有兴趣了解更多详细信息,请阅读本文。

要修复此问题,请以管理员身份打开 Windows PowerShell,并在打开窗口后执行以下命令

Set-ExecutionPolicy Unrestricted

注意:如果您没有管理员权限运行上述命令,您将收到如下错误,该错误会提示您使用“以管理员身份运行”选项打开 Windows PowerShell。

Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
To change the execution policy for the default (LocalMachine) scope, 
start Windows PowerShell with the "Run as administrator" option. To
change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".

此错误意味着什么?

现在我们来看看这个错误意味着什么。这是一个非常常见的错误,并且错误本身说明此系统上禁用了任何脚本的执行,通常,您在任何计算机上首次运行任何 PowerShell 脚本时都会遇到此错误。

PowerShell 中有四种不同的执行策略

  • 受限 (Restricted):如果设置了此策略,则无法执行任何脚本。
  • RemoteSigned:如果设置了此策略,则只能执行从 Internet 下载的脚本,并且这些脚本必须由受信任的发布者签名。
  • 无限制 (Unrestricted):如果设置了此策略,则可以执行任何类型的脚本。
  • 全部签名 (AllSigned):如果设置了此策略,则只能执行由受信任的发布者签名的脚本。

Windows PowerShell 的默认策略是“受限”,因此要运行上述脚本,我们需要使用命令 `Set-ExecutionPolicy Unrestricted` 将其更改为“无限制”。

如果您想为特定用户或当前用户设置无限制,那么如错误中指定,您可以运行以下命令

Set-ExecutionPolicy -Scope CurrentUser

历史

  • 2020 年 12 月 3 日:初始版本
© . All rights reserved.