How to run ng code in visual studio code

The error you're encountering occurs because PowerShell's execution policy is set to restrict the execution of scripts, which is a security measure on Windows systems. To run the Angular CLI (`ng`) or any other scripts via PowerShell, you need to change the execution policy.

Here's how you can fix this issue:

### 1. **Open PowerShell as Administrator**
   - Press `Windows + X` and select "Windows PowerShell (Admin)" or "Terminal (Admin)" depending on your Windows version.

### 2. **Check the Current Execution Policy**
   - In the PowerShell window, type the following command and press `Enter`:
     ```powershell
     Get-ExecutionPolicy
     ```
   - This will show you the current execution policy. The common settings are:
     - `Restricted`: No scripts are allowed to run.
     - `AllSigned`: Only scripts signed by a trusted publisher can be run.
     - `RemoteSigned`: Downloaded scripts must be signed by a trusted publisher.
     - `Unrestricted`: No restrictions; all scripts can run.

### 3. **Change the Execution Policy**
   - To allow scripts to run, you can set the execution policy to `RemoteSigned` or `Unrestricted`. For most users, `RemoteSigned` is recommended as it balances security and functionality.
   - To set it to `RemoteSigned`, run the following command:
     ```powershell
     Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
     ```
   - If you're an administrator and want to apply this setting system-wide, you can change the `-Scope` to `LocalMachine`:
     ```powershell
     Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
     ```
   - After running the command, you might be prompted to confirm the change. Type `Y` and press `Enter`.

### 4. **Try Running the Script Again**
   - Close the current PowerShell window and open a new one (you can use the terminal in VS Code). Now, try running the `ng` command again.

### 5. **Alternative: Use the Command Prompt or Bash**
   - If you're still facing issues, you can use the Command Prompt or Git Bash instead of PowerShell to run Angular CLI commands. These environments don't have the same restrictions on script execution.

### 6. **Revert the Execution Policy (Optional)**
   - If you're concerned about security, you can revert the execution policy to its original state after you've finished running your scripts. For example:
     ```powershell
     Set-ExecutionPolicy Restricted -Scope CurrentUser
     ```
   - This will restore the execution policy to its default restricted state.

Let me know if this resolves your issue!

Share :

How to run ng code in visual studio code