Windows 7上的PowerShell:常规用户的Set-ExecutionPolicy

Dan*_*ton 61 powershell windows-7

我想以普通用户身份在Windows 7上运行PowerShell脚本.每当我尝试时,我都会收到以下错误:

File C:\Users\danv\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because the
execution of scripts is disabled on this system. Please see "get-help about_signing" for
more details.
At line:1 char:2
+ . <<<<  'C:\Users\danv\Documents\WindowsPowerShell\profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)

尝试解决via Set-ExecutionPolicy Unrestricted失败:

PS C:\Users\danv> Set-ExecutionPolicy Unrestricted
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 <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Run Code Online (Sandbox Code Playgroud)

我可以Set-ExecutionPolicy Unrestricted以管理员身份运行该命令,但这似乎不会传播给非管理员用户.

如何以非管理员身份成功运行脚本?

Ste*_*ngs 111

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

这将为当前用户(存储在HKEY_CURRENT_USER中)而不是本地计算机(HKEY_LOCAL_MACHINE)设置执行策略.如果您没有对计算机的管理控制,这将非常有用.

  • 完善.这立即解决了我的问题.令新用户感到沮丧的是,PowerShell被宣传为脚本语言,默认情况下不允许编写脚本. (2认同)
  • “无限制”?请注意 [安全隐患](http://stackoverflow.com/questions/4037939/powershell-says-execution-of-scripts-is-disabled-on-this-system/26955050#26955050)。 (2认同)

Ric*_*ard 63

如果您(或有用的管理员)Set-ExecutionPolicy以管理员身份运行,则将为所有用户设置策略.(我建议使用"remoteSigned"而不是"unrestricted"作为安全措施.)

注意:在64位操作系统上,您需要分别运行Set-ExecutionPolicy32位和64位PowerShell.

  • 对于64位Windows 8的一个注释,您必须从提升的PowerShell窗口执行它,如下所示:`Set-ExecutionPolicy RemoteSigned -Force`后跟`start-job {Set-ExecutionPolicy RemoteSigned -Force} -RunAs32`.这是确保两个版本都得到解决的唯一方法. (19认同)
  • 为32bit-64bit建议+1.它救了我几个小时 (7认同)

小智 7

这应该可以解决您的问题,您应该尝试运行以下命令:

\n\n
Set-ExecutionPolicy\xc2\xa0-ExecutionPolicy\xc2\xa0RemoteSigned\xc2\xa0-Scope\xc2\xa0CurrentUser\xc2\xa0\n
Run Code Online (Sandbox Code Playgroud)\n