How to Fix .ps1 Cannot be Loaded Error | Set Execution Policy
If you are on Windows and are trying to run your npm or pnpm scripts, chances are you might encounter an error that says .ps1 cannot be loaded. This is a PowerShell restrictive error that is generated to stop your system from running unwanted scripts.
The error might look something like this, and generally comes when you are installing or running scripts on your machine:
the error says:
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
that means that your machine is not set to run scripts. This feature is a feature provided by Microsoft to control the loading of configuration files and running scripts to avoid malicious code from affecting your system.
Your organization might have a requirement to abide by Microsoft security features, with specifically defined policies to prevent harmful scripts from running and affecting your system.’
How to Fix .ps1 cannot be Loaded error
Step 1: To tackle this issue, you must first open Windows Powershell in Administrator mode. You can do this by typing PowerShell in your start search bar.
Step 2: Then next to Powershell click on Run as Administrator.
Step 3: A PowerShell window will open with the prefix PS and by default Windows > System32 reflecting in the navigated folder. Something like the following image:
The next thing you have to do is set the execution policy.
Step 4: Type the following and press enter:
Set-ExecutionPolicy RemoteSigned
NOTE: If you want to set the execution policy only for the current user, you can type:
Set-ExecutionPolicy -Scope CurrentUser
instead.
When you press enter you will be asked for a confirmation which might look like this:
At this point it is advisable to go through the Microsoft link on:
https:/go.microsoft.com/fwlink/?LinkID=135170 to understand what the policies mean.
You can change RemoteSigned to other options like: Restricted
, AllSigned
, RemoteSigned
, Unrestricted
Step 5: Press Y or A if you want to change the setting for all.
Step 6: Once you press enter, there will be no confirmation message but the deed will be done. Basically, you will have the same prompt visible. It insinuates that things have happened in the background.
The stricter the policy, the more secure your system becomes.
Alternate way to achieve the above is via C:\Program Files\PowerShell\7\powershell.config.json
using a text editor and modifying the following area.
{
"Microsoft.PowerShell:ExecutionPolicy": "RemoteSigned"
}
Now go ahead and continue running the script on your terminal and check if the error is indeed gone.
If you liked the above tutorial on How to Fix .ps1 cannot be loaded error, please check out other tutorials on DITD as well.