Keywords: PowerShell | Angular CLI | Execution Policy | ng.ps1 | Windows Development
Abstract: This article provides a comprehensive analysis of execution policy restriction errors encountered when running Angular CLI commands in Windows PowerShell. It explores the root causes of these errors and presents multiple solution approaches, with detailed code examples and step-by-step instructions to help developers quickly resolve practical issues in their development environment.
Problem Background and Error Analysis
In Windows operating system environments, many developers encounter a common issue when learning or using the Angular framework: execution policy restriction errors when running Angular CLI commands in PowerShell. The specific error message typically displays:
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
In-depth Analysis of Error Causes
The fundamental cause of this error lies in PowerShell's execution policy security mechanism. As a powerful scripting environment for Windows systems, PowerShell implements strict execution policies by default to prevent malicious script execution. When users install Angular CLI via npm, the system creates a PowerShell script file named ng.ps1 in the C:\Users\%username%\AppData\Roaming\npm\ directory. This file serves as a wrapper script that calls the actual Angular CLI executable.
PowerShell execution policies include several key levels:
- Restricted: Default policy that prohibits all script execution
- RemoteSigned: Allows local script execution but requires digital signatures for remote scripts
- Unrestricted: Permits execution of all scripts
Since most Windows systems default to the Restricted execution policy, when users attempt to run the ng.ps1 script, the system blocks its execution, resulting in the security error described above.
Solution One: Remove ng.ps1 File
Based on the best answer recommendation, the most direct solution involves removing the problematic ng.ps1 file. The core principle of this approach is to bypass PowerShell script execution restrictions by utilizing the cmd executable directly.
Detailed implementation steps:
- Open File Explorer and navigate to:
C:\Users\%username%\AppData\Roaming\npm\ - Locate the file named
ng.ps1 - Right-click the file and select Delete
- Additionally, clear the npm cache by executing:
npm cache clean --force
The working mechanism of this method: After removing ng.ps1, the system falls back to using the ng.cmd file for executing Angular commands. ng.cmd is a batch file that is not subject to PowerShell execution policy restrictions, enabling normal operation in any command-line environment.
Solution Two: Modify Execution Policy
The second solution involves modifying PowerShell's execution policy settings. This approach is more suitable for developers who frequently use PowerShell scripts.
Modify execution policy via PowerShell command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Command component explanation:
Set-ExecutionPolicy: Command to set execution policy-ExecutionPolicy RemoteSigned: Sets execution policy to RemoteSigned-Scope CurrentUser: Applies only to the current user
In Windows 11 systems, execution policy can also be modified through graphical interface:
- Open Settings
- Navigate to Privacy & Security
- Select For developers
- Click Apply in the PowerShell section
This method automatically sets the current user's execution policy to RemoteSigned, allowing local scripts to run without requiring digital signatures.
Solution Comparison and Selection Guidance
Both solutions present distinct advantages and disadvantages:
Advantages of removing ng.ps1 file:
- Simple operation, no administrator privileges required
- Does not affect system security policies
- Immediate effect, no restart needed
Advantages of modifying execution policy:
- Maintains complete Angular CLI functionality
- Enables execution of other PowerShell scripts
- More suitable for heavy PowerShell users
For most beginners and occasional PowerShell users, the first solution is recommended. For professional developers and users requiring frequent PowerShell script usage, the second solution is more appropriate.
Preventive Measures and Best Practices
To prevent similar issues, implement the following preventive measures:
- Check current PowerShell execution policy before installing Angular CLI:
- Consider using Windows Terminal for improved command-line experience
- Regularly update npm and Angular CLI to latest versions
- Standardize command-line tool configurations in team development environments
Get-ExecutionPolicy -List
By understanding PowerShell's security mechanisms and Angular CLI's operational principles, developers can better manage their development environments and enhance productivity.