25
votes

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

8
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
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
As noted in the answer - Restricted is the default PowerShell execution policy.Bill_Stewart
@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

8 Answers

46
votes

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

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

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
17
votes

Open your PowerShell and enter the following command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
6
votes

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

5
votes

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

1
votes

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Then choose A

1
votes

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.

1
votes

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)

0
votes

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

  1. Press the [windows] button and then type PowerShell.
  2. Run as Adiministrator
  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.