75

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 ??

Ken
  • 2,518
  • 2
  • 27
  • 35
  • 4
    What is the output of a `get-executionpolicy` command on one of those VMs? If the output is anything other than RemoteSigned, Unrestricted, or Bypass, that could be your issue. – AdminOfThings Feb 19 '19 at 23:23
  • 1
    an expansion on the question by AdminOfThings ... what is the execution policy on that system for the account that is running the script? – Lee_Dailey Feb 19 '19 at 23:31
  • As noted in the answer - `Restricted` is the default PowerShell execution policy. – Bill_Stewart Feb 20 '19 at 03:01
  • @AdminOfThings I understand that the default is restricted. So why would it be restricted if neither machine has been configured - even according to my group policy ? Both have been in my possession from the get go - New Laptop, New VM (I created).. no changes to policy and in Grp Policy both say Unconfigured. Today the get-executionpolicy is running with out giving me the error - and says exactly - one is restricted and one is not. Yesterday it just spewed out the error that I was not allowed to run scripts on the machine. – Ken Feb 20 '19 at 16:40

15 Answers15

126

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.

Innat
  • 16,113
  • 6
  • 53
  • 101
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • I went into Group Policy and looked at both - both are unconfigured, I have had the laptop brand new 8 months now - never touched policies. The VM I created and at that time I did disable UAC on the VM, could this have caused an issue. I am able to run get-execution policy today (same user) I don't know why that failed yesterday with said error. One more thing - if I am able to just blow by the script execution policy what is the point in having the policy seems any hacker can type the same bypass code I can type: seems more of a nuisance sign that says 'keep out' than anything useful. – Ken Feb 20 '19 at 16:47
  • FYI: The VM is the guest of my host - directly loaded on my host - I am not logging in via a server such as a Vsphere installation. This is just a standalone VM on my laptop. Your answer works today in my VM. I tried yesterday and it just would not. – Ken Feb 20 '19 at 17:04
  • This works if you need IntelliJ to run scripts which it cannot in Windows. Thanks! – CrazedCoder Nov 01 '22 at 18:19
53

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.

Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
crispengari
  • 7,901
  • 7
  • 45
  • 53
29

Open your PowerShell and enter the following command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Saroj Bhattarai
  • 299
  • 3
  • 8
8

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

greybeard
  • 2,249
  • 8
  • 30
  • 66
6

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

Arjjun
  • 1,203
  • 16
  • 15
6

The least problematic approach is to use the command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This will get around Admin user authority issues.

Ron B.
  • 1,091
  • 1
  • 7
  • 13
2

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.

Alexey Vazhnov
  • 1,291
  • 17
  • 20
subhajit das
  • 383
  • 3
  • 8
1

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Then choose A

borchvm
  • 3,533
  • 16
  • 44
  • 45
1

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.

Chamila Maddumage
  • 3,304
  • 2
  • 32
  • 43
1

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.
mk23
  • 1,257
  • 1
  • 12
  • 25
  • There are numerous other answers suggesting to change the execution policy, including several with this exact same `Set-ExecutionPolicy` command, including the [highest-voted and accepted answer](https://stackoverflow.com/a/54776674/150605). – Lance U. Matthews May 21 '22 at 02:06
1

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
Ali Safari
  • 1,535
  • 10
  • 19
0

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

0

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.

0

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

Lucca Ferri
  • 1,308
  • 12
  • 23
Vodka Tony
  • 21
  • 1
0

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

Emilson
  • 11
  • 2