How do you fix Cannot be loaded because running scripts is disabled on this system?

Fix - ng.ps1 cannot be loaded because running scripts is disabled on this system #

The error "ng.ps1 cannot be loaded because running scripts is disabled on this system" occurs when the execution policy does not allow running the specific script on Windows. Use the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command to solve the error.

Open your PowerShell as an administrator and set its execution policy with the Set-ExecutionPolicy command.

Copied!

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

The Set-ExecutionPolicy command sets the PowerShell execution policy for the Windows computer.

The RemoteSigned execution policy is the default execution policy for Windows server computers. It requires that all scripts and configuration files that were downloaded from the internet are signed by a trusted publisher.

This effectively removes the execution policy of Restricted, which doesn't allow us to load configuration files or run scripts. The Restricted execution policy is the default for Windows client computers.

Make sure to open your PowerShell as an administrator before you run the Set-ExecutionPolicy command.

If you aren't able to run the command as an administrator, try running it with the CurrentUser parameter.

Copied!

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

The RemoteSigned policy still prevents us from running unsigned scripts.

Now, run the Get-ExecutionPolicy command:

Copied!

Get-ExecutionPolicy

The Get-ExecutionPolicy command should display the effective execution policy for the current PowerShell session (RemoteSigned).

If you get RemoteSigned back, then you have successfully updated your permissions and are able to run the ng command.

You can also run the command with the -List parameter to display the execution policies for each scope in the order of precedence.

Copied!

Get-ExecutionPolicy -List

When ran with the -List parameter, the command returns a list of all execution policy values for the session listed in precedence order.

The command should show that the RemoteSigned policy is set for the default scope (LocalMachine).

Alternatively, you can try to delete the C:\Users\Your_User_Name\AppData\Roaming\npm\ng.ps1 file and restart your terminal.

Make sure to replace the Your_User_Name placeholder with your actual username.

After restarting your terminal, try running the command again.

If that doesn't help, clear your npm cache and re-run the command.

Copied!

# 👇️ clean npm cache npm cache clean --force

If nothing else works, you can try to set the execution policy to Unrestricted, which means that the system would allow unsigned PowerShell scripts to run.

Copied!

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Starting with PowerShell 6.0, Unrestricted is the default execution policy for non-Windows computers and can't be changed.

The policy loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the internet, you would still get prompted for permission before it runs.

Introduction 

When you have to run your Angular project or any ng command, you may see that the system shows the below error: 

ng: File C:\Users\admin\AppData\Roaming\npm\ng.ps1 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.

At line:1 char:1

+ ng --v

+ ~~

+ CategoryInfo : SecurityError: (:) [], PSSecurityException

+ FullyQualifiedErrorId : unauthorized access

Solution

This error occurs when your system has disabled the running script and your system is can’t accept the ng commands. This error occurs due to security reasons and won't let the script be executed on your system without your approval. Then you have to open the PowerShell with administrative rights.

To solve this problem, you need to follow a few steps:

Step 1

First, you have to need to open the command prompt and run this command.

  1. set-ExecutionPolicy RemoteSigned -Scope CurrentUser 

When you run this command, you can see that your system has set all policies for the current user as remotely. It will take few seconds to complete this process.

The image will be shown like below.

 

Now you have to go to the next step.

Step 2

Now you have to run the second command on your system. This command is:

  1. Get-ExecutionPolicy

When you have run this command your system has a show “RemoteSigned”. If you have received this message, then your problem will be solved. Now you have to go to the next step to view the list of policy which policy has been updated by the last commands.

The image will be shown as below:

How do you fix Cannot be loaded because running scripts is disabled on this system?

Step 3

To view their policy, you need to run this command in your command prompt:

  1. Get-ExecutionPolicy -list  

When you run this command, a few policies are shown on your monitor screen. These policies are:

 

Scope ExecutionPolicy

----- ---------------

MachinePolicy Undefined

UserPolicy Undefined

Process Undefined

CurrentUser RemoteSigned

LocalMachine Undefined

CurrentUser RemoteSigned

LocalMachine Undefined

Now you can see your system execution policy list. You have successfully solved your problem. Now you need to go to the command prompt and check that your problem is solved.

Now I run an ng command in my command prompt. This command is “ ng --version”. To check my CLI version. When I run it, the ng command the output will be shown like this:

OUTPUT

You can see that the ng command has been run successfully. Now my problem is solved.  

I hope you enjoyed this article. To learn more about Angular, follow C#Chamangautam, and to learn more technology, follow C#Corner.

Why is running scripts disabled on this system?

While running PowerShell script, if you get running scripts is disabled on this system, it is because the PowerShell execution policy is set up by default as Restricted and doesn't allow to run script. PowerShell has built-in security features implemented.

Can't be loaded because running scripts is disabled on this system?

The error "ng. ps1 cannot be loaded because running scripts is disabled on this system" occurs when the execution policy does not allow running the specific script on Windows. Use the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command to solve the error.

How do I enable running scripts on my system?

* Note: If you still receive the error "Scripts are disabled on this system", give one of the following commands and try again to run your script: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned. set-executionpolicy remotesigned.