npm.ps1 Cannot Be Loaded Because Running Scripts Is Disabled on This System

Windows logo

Short answer: “npm.ps1 cannot be loaded because running scripts is disabled on this system” means PowerShell’s execution policy on that Windows PC is still at the default, Restricted, so the npm command (which is a PowerShell script) can’t run. Set the policy to RemoteSigned for your own user account with Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser, then reopen your terminal. If the app you build is then stopped by Windows Security with “Action blocked”, that is a separate Microsoft Defender rule, covered below.

A staff member at a manufacturing business had just been given a new laptop and wanted to carry on building an internal app in Visual Studio Code. On his old computer npm install worked, but on the new one the VS Code terminal refused with “running scripts is disabled on this system”. We changed the PowerShell execution policy for his account only, and npm worked straight away. The next day he hit a second block: the app he had built ran fine once, but after a rebuild Windows Security said “Action blocked”. That turned out to be a Microsoft Defender attack surface reduction rule, and the fix was to build in a folder that was already approved for that app. Here is what we found and how to fix both.

Fixed by Elliot Munro, CISO, and Emma Mitchell, Senior Engineer at GCIT. Written up by Elliot Munro.

Symptoms

When you run npm install, npm run or npx in PowerShell or the Visual Studio Code terminal on Windows, it fails with:

npm : File C:\Program Files\nodejs\npm.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.

Followed by:

CategoryInfo : SecurityError: (:) [], PSSecurityException
FullyQualifiedErrorId : UnauthorizedAccess

The same message can appear for yarn.ps1, pnpm.ps1, ng.ps1 or any other tool installed as a PowerShell script. It usually shows up on a new or rebuilt computer, and the same command works in Command Prompt.

Later, when you run an app you have built yourself (for example an Electron or Node app packaged as an .exe), you may see this Windows Security notification instead:

Action blocked. Your administrator caused Windows Security to block this action. Contact your help desk.

Windows Security notification: Action blocked. Your administrator caused Windows Security to block this action. Contact your help desk.

Affected Software

  • Application: npm and Node.js, and other command-line tools that install a .ps1 wrapper
  • Platform: Windows 10 and Windows 11, Windows PowerShell 5.1 and PowerShell 7, the Visual Studio Code integrated terminal
  • Related Services: Microsoft Defender for Endpoint attack surface reduction (ASR) rules, Microsoft Intune

Cause

“Running scripts is disabled on this system”

When Node.js is installed on Windows, the npm command is provided as both npm.cmd and npm.ps1. PowerShell prefers the .ps1 version. On Windows client computers, PowerShell’s default execution policy is Restricted, which allows individual commands but no script files at all, so npm.ps1 is refused. Your old computer probably had the policy changed at some point, which is why it worked there.

In our case, Get-ExecutionPolicy -List showed every scope as Undefined, meaning nothing had set a policy and the Restricted default applied. There was no Group Policy or Intune setting involved.

“Action blocked” when you run your own app

This one has nothing to do with PowerShell or admin rights. Many businesses enable the Microsoft Defender ASR rule Block executable files from running unless they meet a prevalence, age, or trusted list criterion. It blocks .exe files that are new and rare, which is exactly what a program you have just built is. Being a local administrator doesn’t get around it.

The Defender event log (Microsoft-Windows-Windows Defender/Operational) records it as event ID 1121, with the rule ID 01443614-cd74-433a-b99e-2ecdc07bfc25 and the path of the blocked file. In our case the project lived on the OneDrive Desktop, outside the folder that had already been excluded for that app.

Resolution

Fix the execution policy for your account

  1. Open PowerShell, or a new terminal in Visual Studio Code. Admin rights aren’t needed.
  2. Check the current settings:
    Get-ExecutionPolicy -List
  3. If MachinePolicy or UserPolicy shows a value, the policy is set by Group Policy or Intune and you need to ask IT to change it there. Otherwise, set RemoteSigned for your own account:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  4. Confirm with Y if asked, then close and reopen Visual Studio Code or your terminal.
  5. Run npm -v to check npm now runs, then try npm install again.

As a quick workaround while you wait, you can run npm.cmd install instead of npm install, or switch the VS Code terminal to Command Prompt. Both use the .cmd version and skip PowerShell’s script check.

Fix “Action blocked” for an app you build

  1. Ask IT to check the Defender event log on your computer for event ID 1121 at the time of the block. It shows which rule fired and the exact file path.
  2. If it is the prevalence, age or trusted list rule, the safest fix is to build and run the app from a fixed project folder that IT has added as an ASR exclusion, for example a folder under C:\ that only that project uses. In our case an exclusion already existed for the right folder, so we moved the project there with no policy change at all.
  3. Avoid excluding your Desktop, Downloads or OneDrive folders. They are user-writable and sync between devices, so an exclusion there would let any new program run.
  4. If your project is in Git, cloning it into the approved folder and running npm install again is the quickest way to move it. Build output and node_modules don’t need to be copied.

Important Notes

  • Use CurrentUser, not LocalMachine. Setting the policy for your own account fixes npm without changing security for everyone else on the computer and without admin rights.
  • RemoteSigned is still protective. It lets local scripts run but still blocks unsigned scripts downloaded from the internet, which are marked with the Mark of the Web.
  • Don’t use Bypass or Unrestricted as a permanent setting. They remove the protection entirely.
  • The execution policy isn’t a security boundary for attackers. It stops accidental script runs. ASR and application control are what actually stop untrusted programs, so don’t ask for those to be turned off to make development easier. Ask for a narrow exclusion instead.
  • Keep your project out of OneDrive if you can. A node_modules folder can hold hundreds of megabytes, and syncing it slows OneDrive down for no benefit.

If your developers keep running into security blocks on new computers, get in touch with our team and we’ll set up a safe, repeatable developer build.

Was this article helpful?
Ready to secure and simplify your IT? Talk to a GCIT expert today.