PowerShell:因为在此系统上禁⽌运⾏脚本,解决⽅法
在⾃⼰编PowerShell脚本的时候突然遇到这个问题:
⽆法加载⽂件 C:\Users\DH\Desktop\cs\rename.ps1,因为在此系统上禁⽌运⾏脚本。有关详细信息,请参阅
https:/go.microsoft/fwlink/?LinkID=135170 中的 about_Execution_Policies。
+ CategoryInfo : SecurityError: (:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
问题截图
查了查之后发现是在计算机上启动 Windows PowerShell 时,执⾏策略很可能是 Restricted(默认设置)。
Restricted 执⾏策略不允许任何脚本运⾏。
AllSigned 和 RemoteSigned 执⾏策略可防⽌ Windows PowerShell 运⾏没有数字签名的脚本。
本主题说明如何运⾏所选未签名脚本(即使在执⾏策略为 RemoteSigned 的情况下),还说明如何对  脚本进⾏签名以便您⾃⼰使⽤。
有关 Windows PowerShell 执⾏策略的详细信息,请参阅 about_Execution_Policy。
get-executionpolicy
想了解 计算机上的现⽤执⾏策略,打开PowerShell 然后输⼊ get-executionpolicy
powershell怎么以管理员身份运行现⽤执⾏策略
set-executionpolicy remotesigned
以管理员⾝份打开PowerShell 输⼊ set-executionpolicy remotesigned
选择Y 然后电脑上就可以执⾏⾃⼰编写的脚本⽂件