[Fix] cannot be loaded because the execution of scripts is disabled on this system

The error “cannot be loaded because the execution of scripts is disabled on this system.” is an error that is invoked whenever you run a command file that calls a PowerShell script from cmd.exe. Given below is the snippet of the error:

cannot be loaded because the execution of scripts is disabled on this system.

I would like to share with you the steps I took to fix the “cannot be loaded because the execution of scripts is disabled on this system.” in your project file.

Why “cannot be loaded because the execution of scripts is disabled on this system.” error is seen?

The error, “cannot be loaded because the execution of scripts is disabled on this system.” is seen because; you must not have set the execution policy for the host which you are using.

In case you are using Windows Server 2008 R2 for your project, then there is an x64 as well as a x86 version of PowerShell both of which have to have their execution policies set.

The detailed solution to fix the error “cannot be loaded because the execution of scripts is disabled on this system.”, is given below:

How to fix the error, “cannot be loaded because the execution of scripts is disabled on this system.”?

To fix this error, you will have to set the correct execution policy for the host you are using. You can also fix the error by completely by-pasing the policy by following the correct command.

Error “cannot be loaded because the execution of scripts is disabled on this system.” can be fixed by setting the correct execution policy for the host you are using or you can also fix the error by completely by-pasing the policy by following the correct command.

To fix the error, “cannot be loaded because the execution of scripts is disabled on this system.”, follow one of the below mentioned steps:

Method 1: Set the correct execution policy.

To fix the error, “cannot be loaded because the execution of scripts is disabled on this system.”, first, you will have to set the execution policy for the host which you are using .

In case you are using Windows Server 2008 R2 for your project, then there is an x64 as well as a x86 version of PowerShell both of which have to have their execution policies set

Set yourself as an Administrator, then you can set the execution policy by typing the below mentioned code in your PowerShell window:

Set-ExecutionPolicy RemoteSigned

If you want to know more about it, you can read more about it Using the Set-ExecutionPolicy Cmdlet, you can find it link here.

When you have completed with your project file, you will have to set the policy back to its default value with:

Set-ExecutionPolicy Restricted

Once you set this you may encounter an error saying:

Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
To change the execution policy for the default (LocalMachine) scope, 
  start Windows PowerShell with the "Run as administrator" option. 
To change the execution policy for the current user, 
  run "Set-ExecutionPolicy -Scope CurrentUser".

To fix this error you will have to run the below mentioned command:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This should fix the error, “cannot be loaded because the execution of scripts is disabled on this system.”.

Method 2: Bypass the policy

If the error, “cannot be loaded because the execution of scripts is disabled on this system.” is not fixed by following the above step, you can also bypass the policy completely.

In order to bypass this policy for a single file you will have to add -ExecutionPolicy Bypass when you are running PowerShell like given below:

powershell -ExecutionPolicy Bypass -File script.ps1

This should fix the error, “cannot be loaded because the execution of scripts is disabled on this system.”.

Conclusion

To fix the error “cannot be loaded because the execution of scripts is disabled on this system.”, you will have to set the correct execution policy for the host you are using. You can also fix the error by completely by-pasing the policy by following the correct command.