The error occurs because the PowerShell execution policy is blocking scripts like npm.ps1. To fix it, either change the execution policy to allow scripts, or unblock the specific script.
Option 1: Change execution policy (recommended for development machines)
- Open PowerShell as Administrator.
- Check the current policy:
Get-ExecutionPolicy - Set a less restrictive policy, for example
RemoteSignedfor the current user (no admin needed for this scope):
If changing for the whole machine (requires admin):Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserSet-ExecutionPolicy -ExecutionPolicy RemoteSigned - Close and reopen PowerShell, then run:
npm init -y
RemoteSigned allows local scripts to run and requires downloaded scripts to be signed by a trusted publisher.
Option 2: Allow scripts only for this session/process
If changing the global or user policy is not desired:
- Start PowerShell with a relaxed policy just for this process:
or inside an existing session:pwsh.exe -ExecutionPolicy RemoteSignedSet-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned - Run
npm init -yin that session. The change applies only to that PowerShell process.
Option 3: Unblock the specific script file
If the script is blocked because it is marked as downloaded from the internet and the policy is RemoteSigned:
- In PowerShell, run:
If aGet-Item "C:\Program Files\nodejs\npm.ps1" -Stream "Zone.Identifier" -ErrorAction SilentlyContinueZone.Identifierstream exists, unblock it:Unblock-File -Path "C:\Program Files\nodejs\npm.ps1" - Run
npm init -yagain.
Before changing execution policies or unblocking files, review the security implications and ensure the scripts being run are trusted.
References: