Kushal Solanki
Kushal Solanki

Reputation: 127

Getting Set-ExecutionPolicy error while running any powershell script

I am getting below error while running any powershell script. It's happening on only one of the client's servers. I am not sure what is triggering this command.

If I change this registry key from RemoteSignedto to ByPass error goes away.

Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass

For example I have below simple script of one line.

Read-Host -Prompt "Hit Enter to exit"
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope.  Due to the override, your shell will retain its current effective
execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more
information please see "Get-Help Set-ExecutionPolicy".
At line:1 char:46
+ if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process  ...
+                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Result from Get-ExecutionPolicy -List

Scope                 ExecutionPolicy
-----                 ---------------
MachinePolicy         RemoteSigned
UserPolicy            RemoteSigned
Process               Undefined
CurrentUser           Undefined
LocalMachine          Unrestricted

Upvotes: 3

Views: 11021

Answers (1)

Nirav Mistry
Nirav Mistry

Reputation: 989

You need to Run as Administrator and then try to Set-ExecutionPolicy..

or you can run powershell by this way also :

powershell.exe -ExecutionPolicy bypass

or

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

Upvotes: 6

Related Questions