198

I have started to learn Angular but I note that powershell in Windows gives me an error whenever I make an angular command like:

ng new new-app

or

ng serve

this is the error what I got:

ng : File C:\Users\< username >\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 serve
+ ~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

P.S. I try these commands in cmd and it works.

Amir Makram
  • 7,338
  • 3
  • 10
  • 19
  • Can you run `Get-ExecutionPolicy` and provide the output? Additionally can you run to `C:\Users\< username >\AppData\Roaming\npm` and open the properties for `ng.ps1`, to see if there is a tick box which states "blocked", if so untick this. – Jacob Sep 21 '19 at 00:31
  • 3
    I think the best way is removing ng.ps1 when I did everything has worked again – Amir Makram Sep 21 '19 at 11:32
  • 2
    @TheFabio Actually May I will never know, after deleting ng.ps1 everything was going in perfect way. – Amir Makram Nov 05 '19 at 18:04

4 Answers4

516

Remove ng.ps1 from the directory C:\Users\%username%\AppData\Roaming\npm\ then try clearing the npm cache at C:\Users\%username%\AppData\Roaming\npm-cache\

ErraticFox
  • 1,026
  • 1
  • 12
  • 28
Amir Makram
  • 7,338
  • 3
  • 10
  • 19
  • 91
    FYI clearing the cache was not necessary for me. – Edwin Lambregts Nov 01 '19 at 13:59
  • 2
    This is SPOT-ON! – doncadavona Nov 14 '19 at 15:38
  • 3
    Great, but why? Is it left over from a previous Angular installation and no longer being called correctly? Why does it no longer work? – Patrick Nov 20 '19 at 04:55
  • 2
    Actually, I don't know. I don't even found any mention for this file anywhere so I thought that file really not necessary, so I just delete it – Amir Makram Nov 20 '19 at 13:22
  • 4
    worked without clearing the cashe – amal50 Nov 24 '19 at 16:04
  • I have the "ng.ps1" (and "ng.cmd" and "ng", and "node_modules" folder) in C:\Users\[username]\AppData\Roaming\npm folder. They were (re)created 2 days ago when I run ng update (I checked the modified date). After that update (current 8.3.21, previous: 8.3.xx) the ng stop to work with Powershell. Deleting it (renamed) solved the problem. No cache cleaning is needed but in VS Code you need to kill and reopen the Terminal. Maybe Angular introduced the "ng" command directly as a powershell script-let but that resulted in this common permission problem ?! – Alex 75 Dec 23 '19 at 00:24
  • Also works for me without clearing cache – mgrotheer Jan 23 '20 at 18:21
  • works like a charm – OOD Waterball Mar 13 '20 at 05:18
  • You don't need to do this - see the answer below https://stackoverflow.com/a/58710158/834431 – Chris Apr 01 '20 at 12:06
  • This was spot on for me too. I also did not have to clear cache. Although I just now installed @angular/cli before running the command to follow a book I am reading. – Sam Apr 05 '20 at 12:01
  • Worked without needing to clear the npm cache. – Kamran May 06 '20 at 05:20
  • If you have ERROR 'More than one module matches. Use skip-import option to skip importing the component into the closest module.', please use `ng g c new-component --module app` Good Luck – Mojtaba Nava Jul 11 '20 at 08:54
  • Worked fine without clearing the cache, thanks! – janv Aug 31 '20 at 16:25
  • Just clearing the cache worked for me lol. – Tarik Ćosović Dec 06 '20 at 21:21
368

I solved my problem by running below command

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Stanley Mohlala
  • 4,024
  • 1
  • 8
  • 18
34

script1.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170

This error happens due to a security measure which won't let scripts be executed on your system without you having approved of it. You can do so by opening up a powershell with administrative rights (search for powershell in the main menu and select Run as administrator from the context menu) and entering:

set-executionpolicy remotesigned
Ihor Patsian
  • 1,258
  • 2
  • 15
  • 23
Hozaifa
  • 357
  • 2
  • 3
-1

open windows powershell, run as administrater and SetExecution policy as Unrestricted then it will work.