Powershell Script Execution Policy Change and Fixing the Error “running scripts is disabled on this system”

Powershell Script Execution Policy Change and Fixing the Error “running scripts is disabled on this system”

 set-execution-policy-running-scripts-is-disabled

Powershell by Windows is the relative equivalent of Linux terminal, with reference to what you can do with the two tools. They are both powerful with the ability to; run commands that effect changes on the respective host environments, create scripts, as well as run them. Now, I know that any Linux user will definitely argue against this, and in such a debate, Linux Terminal always wins (I am one of the critics of Windows). Besides, the Microsoft fraternity also thought so and included WSL (Windows Subsystem for Linux), so who is anybody to stand against the undisputed King Linux. Did you also know that Microsoft respects Linux to the extent of using their custom Linux distribution on their own Azure cloud infrastructure? [THAT’S A DEBATE FOR ANOTHER DAY AND ARTICLE]

Have you ever tried to run a script on Microsoft Windows Powershell and run into the error “running scripts is disabled on this system“? That is a common error that you run to when you trigger Windows security against scripts. By default, running scripts is disabled unless you explicitly decide to override that. I’m gonna show you how.

In this article, I used “yarn” package manager command, [already bundled in my system by corepack that comes with Node.js (though not enabled)] to run a script;

yarn install

The above command runs a script to install all yarn dependencies in my system, and that was enough to trip the wire (yarn is just like npm, a popular and the official package manager for Node.js). Immediately I was gifted with the error:

yarn : File C:\Program Files\nodejs\yarn.ps1 cannot be loaded because running scripts is disabled
on this system.

 The error tells you that the command I ran triggered a script , yarn.ps1, to install the dependencies required. PS1 is the extension used for Powershell scripts. To fix the error, you need to run the remedy to tell Windows to stop trippin’, ’cause we actually know what we doing, so we do not need all that parental oversight ****:

Set-ExecutionPolicy Unrestricted

Now, it is good to know that you can pass other arguments on the command “Set-ExecutionPolicy“, including; “RemoteSigned”, “AllSigned”, “Restricted”, “Default”, “Bypass” or “Undefined”. Each of the arguments\values (trying to immitate windows funny directory referencing) has a different implication that you can research on official Microsoft Windows documentation.

At this point, Windows tells us that as much as we do not need supervision, the action we are taking is for big boys and thus we need Administrator privileges (that’s ’cause we want to modify some registry entries). It throws the following error:

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To
change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the 
execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".

set-execution-access-to-the-registry-is-denied

Script Execution Policy Change 

You just need to open Powershell as an Administrator and all will go smoothly after that. All you need to do is confirm that you wanna make the change by literally typing in a letter corresponding to your intended action, in this case “Y” or “A”.

set-execution-unrestricted-administrator-access

Now you can run your script (in this case “yarn install”) or any other script that landed you to the error that got you researching, just to land on thetqweb

SEE Y'ALL ON YOUR NEXT PROBLEM(S).

Powershell Script Execution Policy & Fixing Runtime Errors
Hacking | thetqweb