32

When I try to run ionic commands like ionic serve on the VS Code terminal, it gives the following error.

How can I fix this?

ionic : File C:\Users\Lakshan\AppData\Roaming\npm\ionic.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
+ ~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44

4 Answers4

105

I found a way to fix this error.

It is as follows:

  1. First, Open PowerShell with Run as Administrator.
  2. Then, run this command in PowerShell
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    
  3. After that type Y and press Enter.
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Tharindu Lakshan
  • 3,995
  • 6
  • 24
  • 44
  • 13
    The "Unrestricted" execution policy is generally considered risky. A better choice would be "Remote-Signed", which doesn't block scripts created and stored locally, but does prevent scripts downloaded from the internet from running unless you specifically check and unblock them. – Jeff Zeitlin Nov 01 '20 at 16:25
  • Read more about Execution Policies (AllSigned, Bypass, Default, RemoteSigned, Restricted, Undefined, Unrestricted) https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2 To remove the execution policy for a particular scope, set the execution policy to *Undefined*. – Effie Nov 03 '22 at 10:29
9

This is because of Execution Policy. This defines how powershell scripts will run.

In Default windows desktops, it is Restricted, not allowing any scripts (signed or unsigned) only interactive sessions.

So best is you set using RemoteSigned (Default on Windows Server) letting only signed scripts from remote and unsigned in local to run, but Unrestriced is insecure lettting all scripts to run.

To set run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned as administrator.

Wasif
  • 14,755
  • 3
  • 14
  • 34
5

This code will fix it:

Set-ExecutionPolicy RemoteSigned –Scope Process
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Medo Rawesh
  • 51
  • 1
  • 1
3

To Bypass this you need to change the Execution policy. Type this into your terminal.

Set-ExecutionPolicy -Scope CurrentUser

then it will prompt you to supply a value at which point you can set Bypass / RemoteSigned or Restricted.

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: "RemoteSigned" or "Bypass" or "Restricted".
Relie Essom
  • 959
  • 9
  • 15