3

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
5
  • This is likely a Group Policy setting. Is this server in a domain? What is the result of Get-ExecutionPolicy -List? Commented Jul 22, 2019 at 17:32
  • @JeffZeitlin have modified the question to list execution policy. Thanks. Commented Jul 22, 2019 at 18:02
  • Have you tried to run as admin and then set-execution policy ? Commented Jul 23, 2019 at 4:44
  • @NirajGajjar yes if I set execution policy error goes away. What I wanted to know is from where that error is getting triggered. Thanks. Commented Jul 24, 2019 at 7:08
  • @KushalSolanki by default set execution policy for all users, if you are not an admin then it does not allow you to change it but you can change policy for only your profile ,... Commented Jul 24, 2019 at 7:23

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Niraj, I am trying to do 2 things. Run script with admin rights (as described by Jonathan in stackoverflow.com/questions/7690994/…) and then set execution policy to bypass. Problem is I cant do that without getting an error. I don't want to do that manually. There are lot of other techs who will be using this script. Thanks, Kushal

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.