Ken
Ken

Reputation: 2808

PowerShell bug “execution of scripts is disabled on this system.”

I have a power shell script that runs to stop services, 'stop / terminate process' , delete 2 files and then restart.

I can run this script perfect on my Windows 10 64 Bit Host Machine - with ZERO issues. I try to run it in my Virtual Machines and I get the error

cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170

SO just for giggles I went to see my group policies and they are not configured on either machine.

Administrative Templates > Windows Components > Windows PowerShell Not Configured.

So why the issue on the virtual machine and not in my host ?

EDIT Ran Get-ExecutionPolicy and also Get-ExecutionPolicy-List on VM Restricted

MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

Ran it on my Host

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

Group Policy Screen Shot

I do not know how my local machine was changed - software installation ??

Upvotes: 98

Views: 316094

Answers (16)

John Carter
John Carter

Reputation: 343

Windows settings worked for me:

Go to Settings > System > For developers:

Windows Settings

Go to Powershell and enable the Change execution policy ... option:

Powershell Option

Upvotes: 2

crispengari
crispengari

Reputation: 9321

If you are on Windows here is what you have to follow:

  1. Press the [windows] button and then type PowerShell.
  2. Run as Administrator
  3. Copy and Paste the following command and hit [Enter]
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
  1. Type Y and hit [Enter]
  2. Rerun the command and type A hit [Enter]
  3. Close the powershell and try again

Good luck.

Upvotes: 78

AdminOfThings
AdminOfThings

Reputation: 25001

The following will allow all local scripts to execute on the VM, irrespective of whether they're signed or not:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

I am going to go out on a limb here and just rehash a portion of About Execution Policies.

The default execution policy for Windows client OSes is Restricted. This means that a script will not run automatically. If your VM has a Windows client OS and you have never changed the execution policy, then your issue is expected. If the one Windows 10 machine works without issues, then someone changed the execution policy.

On the problematic VMs, you will need to determine the scope (or account) that is running your script. Then you will need to set the execution policy accordingly.

If you are testing running a script while logged into the server as yourself, then you can just open a PowerShell console and run the following:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Then run the script in that same console.

The following command will list the execution policy for all scopes on that machine:

Get-ExecutionPolicy -List

You should compare the command above on the working system and the non-working system. Your issue is likely be the execution policy setting for the particular scope that is running the script. If you read the link in my post, it should help you determine what you need to change specifically.

Upvotes: 153

Ali Safari
Ali Safari

Reputation: 1775

I have tried these two ways that you can tackle this problem succesfully:

  1. By enabling PowerShell execution policies:

    PS C:\Users\usr1>Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

For example, I encountered this problem when i wanted to run yarn command at my project's root folder. Then by running the command above, I was abled to run the comand yarn without a problem. enter image description here

  1. Use bash terminal instead of powershell terminal You can use this method when you get error from the powershell terminal by running the Set-ExecutionPolicy command. Like the error below: enter image description here In such case, by switching your terminal to bash you can have your scripts run: enter image description here

Upvotes: 1

subhajit das
subhajit das

Reputation: 445

After running powershell as administrator, run the following commands:

  1. Get-ExecutionPolicy -List
  2. Set-ExecutionPolicy Unrestricted
  3. Set-ExecutionPolicy Unrestricted -Force

May be you need to restart the machine.

Upvotes: 2

Emilson
Emilson

Reputation: 11

Use the syntax below

Set-ExecutionPolicy -Scope CurrentUser

Enter the "Unrestricted" as your value in the ExecutionPolicy parameter syntax below:

ExecutionPolicy:Unrestricted

Then run your command; you can check the node version or any version of the software you installed after implementing the commands above

Upvotes: 0

mk23
mk23

Reputation: 1403

I had same issue when trying to create a vue application by running vue create my-project

To fix this I have followed below steps

  1. Open powershell as an administrator on Windows
  2. Run this command - Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
  3. Now open a new session on your terminal and run your application specific script. It worked.

Upvotes: 1

Vodka Tony
Vodka Tony

Reputation: 21

user powerShell as admin and execute the following commends:

PS C:\WINDOWS\system32> Get-ExecutionPolicy -List

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

To unrestrict the execution policy:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

Under your normal user. The following requires to open an administrator instance:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine

as an administrator.

You might need to restart the computer afterwards.

You will see peerjs server is working

Upvotes: 0

Etienne Salimbeni
Etienne Salimbeni

Reputation: 582

I m just farwarding all the changes that implies with setting the ExcecutionPolicy to RemoteSigned with the command : Set-ExecutionPolicy -ExecutionPolicy RemoteSigned ( I really recommend to first give a look at the other policies offered : Windows Execution Policies )

RemoteSigned :

  • The default execution policy for Windows server computers.
  • Scripts can run.
  • Requires a digital signature from a trusted publisher on scripts and configuration files that are downloaded from the internet which includes email and instant messaging programs.
  • Doesn't require digital signatures on scripts that are written on the local computer and not downloaded from the internet.
  • Runs scripts that are downloaded from the internet and not signed.
  • Risks running unsigned scripts from sources other than the internet and signed scripts that could be malicious.

Note that on Windows, browsers sign downloaded files and mark them as 'coming from the Internet'. If u want to unblock such a script, u can use the cmd Unblock-File.

Upvotes: 0

mehmd bädawy
mehmd bädawy

Reputation: 1

user powerShell as admin and execute the following commends:

1-Set-ExecutionPolicy RemoteSigned

2-Get-ExecutionPolicy -List

3-Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine*

and rerun the scrips again and it will work

Upvotes: 0

Ron B.
Ron B.

Reputation: 1101

The least problematic approach is to use the command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This will get around Admin user authority issues.

Upvotes: 7

Chamila Maddumage
Chamila Maddumage

Reputation: 3866

I had the same issue in my PC. Open the windows PowerShell ISE in administrator mode and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine This command solved my issue.

Upvotes: 1

Soha darvish haghjoo
Soha darvish haghjoo

Reputation: 91

I had the same problem with VS Code then I check the cmd with Administrator run. There is no problem so better to use CMD easy way to pass this problem screen shot of circumventing security setting

Upvotes: 9

Mo'men Alhussaini
Mo'men Alhussaini

Reputation: 11

Open your powershell as an administrator and then paste those commands:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Then choose A

Upvotes: 1

Saroj Bhattarai
Saroj Bhattarai

Reputation: 299

Open your PowerShell and enter the following command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Upvotes: 29

Arjjun
Arjjun

Reputation: 1301

Run Powershell as an administrator and run the following command: set-executionpolicy remotesigned

Upvotes: 6

Related Questions