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
Kushal Solanki
  • 127
  • 1
  • 13

1 Answers1

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
Nirav Mistry
  • 949
  • 5
  • 15
  • Hi Niraj, I am trying to do 2 things. Run script with admin rights (as described by Jonathan in https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator) 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 – Kushal Solanki Jul 25 '19 at 14:23