Modifying Execution Policy to Enable PowerShell to Run Scripts

In the Windows system, PowerShell supports a richer set of commands and modules compared to the traditional cmd command line. However, developers may encounter issues with PowerShell not executing commands. Here's my experience.

Issue

I have a development project managed by NPM. To upgrade the project's dependency package versions, I navigated to the project path in PowerShell and executed the npm-check-updates command. However, PowerShell returned an error message saying, "File cannot be loaded because running scripts is disabled on this system."

At first, I thought it was a software environment configuration issue. But then I confirmed that npm-check-updates was correctly installed globally, and its path was added to the system's environment variables. When I tried running the same command in Command Prompt, it worked normally! However, I didn't want to give up the convenience of running commands in PowerShell.

Based on the error message, I found a solution.

Solution

In PowerShell, execute this command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This command takes effect immediately, and then you can run various global NPM commands in PowerShell normally.

Explanation

It was PowerShell's execution policy that prevented the command from executing.

The PowerShell execution policy is a security feature that controls the conditions under which configuration files and scripts are loaded and run. This feature helps prevent the execution of malicious scripts.

You can view detailed information about PowerShell execution policies here.

You can retrieve the current effective execution policy with the following command:

Get-ExecutionPolicy

The default execution policy for Windows client systems is Restricted, which means it prevents the execution of all script files, including format and configuration files (.ps1xml), module script files (.psm1), and PowerShell configuration files (.ps1). Therefore, the global commands of the NPM package were blocked by PowerShell.

To set an execution policy for a specified scope, execute the following command:

Set-ExecutionPolicy -ExecutionPolicy <PolicyName> -Scope <scope>

Where <PolicyName> and <scope> need to be replaced with the corresponding values, which can be found in the detailed information in the link above.

The command in the previous solution applies the RemoteSigned policy to the current user. This is a way to execute scripts with lower privileges.

To remove the current user's execution policy and restore the system's default state, you can execute the following command:

Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser

Profile picture

A curious adventurer who loves tech and art. Recording life's moments with words, sharing observations and reflections, experiences and insights. Face challenges fearlessly.