无法加载文件 C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。

问题:打开 PowerShell,提示如下类似报错信息:

1
2
3
4
5
6
7
8
9
10
11
Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

. : 无法加载文件 C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参
阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException

原因:Windows 下 PowerShell 的脚本执行策略的默认设置为 Restricted

1
2
PS C:\Windows\system32> get-ExecutionPolicy
Restricted

解决:以管理员身份运行 PowerShell,修改脚本执行策略:

1
PS C:\Windows\system32> set-ExecutionPolicy RemoteSigned