Fix File cannot be loaded because running scripts is disabled on this system in Powershell

When you try to run a Powershell script (ps1), you may face the following error: “File cannot be loaded because running scripts is disabled on this system.”

File cannot be loaded because running scripts is disabled on this system

This blog post explains:

  • why you get this error.
  • what your options are to keep Powershell safe.
  • how to fix it.

In a hurry? It’s not necessary to read the reason for this error. You may immediately skip to Fixes to File cannot be loaded because running scripts is disabled on this system

Cause

Powershell has built-in security features. One of them is the so-called Execution Policy. This policy can prevent scripts from running and can become useful in certain situations. For instance, in high-secure environments where running unknown Powershell scripts may be risky.  To view the current systemwide Execution Policy setting, type the following in Powershell:
Get-ExecutionPolicy

4 levels of security

Before you focus on fixing, it’s good to know the options. It may save you from using a risky or insecure setting. There are 4 policy levels to choose from. From most secure to most insecure:

  • Restricted: No Powershell scripts can be run. This is the default setting.
  • AllSigned: Scripts can be run, but all must have a digital signature. Even if you wrote the script yourself on the local computer.
  • RemoteSigned: Locally-written scripts can be run. But scripts from outside (email, IM, Internet) must be signed by a trusted publisher.
  • Unrestricted: Any script will run. Regardless of who created them or whether they are signed. 

In the fixes, I assume you have downloaded the script. Thus, I provide the Unrestricted option in each solution. But if you wrote the script yourself on your current pc, you’re better off using RemoteSigned.

Fixes to File cannot be loaded because running scripts is disabled on this system

There are several ways to fix the error “File cannot be loaded because running scripts is disabled on this system.” Each has its pros and cons. But whatever option you choose, it takes care of your problem anyway.
As I noted in 4 levels of security, Unrestricted is always a solution, but RemoteSigned is a more secure option.
All fixes are OS independent. They will work on Windows 10, Windows 2016, Windows 2019. And even on Windows 7.

Set ExecutionPolicy on computer (requires administrator privileges)

To set the ExecutionPolicy for all users on your computer:

  1. Open Powershell as Administrator
  2. Type Set-ExecutionPolicy Unrestricted and hit Enter.
  3. Confirm the Execution Policy Change by typing ‘Y‘ and hit Enter.
  4. Done! Everyone on the computer should now be able to run the script.

Note: once you have set the Execution Policy, it applies to all Powershell sessions in all applications. Do you use Visual Studio Code or Powershell ISE? The new setting also applies to those apps!

Set ExecutionPolicy on user

You don’t have Administrator privileges on your computer? You can still set the ExecutionPolicy to Unrestricted on the scope of the user. Here’s how:

  1. Open Powershell.
  2. Type Set-ExecutionPolicy Unrestricted -Scope CurrentUser and hit Enter.
  3. Confirm the Execution Policy Change by typing ‘Y‘ and hit Enter.
  4. Done! Your user account should now be able to run the script.

Run a Powershell session with ExecutionPolicy Bypass

To lower the security of Powershell for one time only, you can use the Bypass switch. This allows you to run a script temporarily while keeping the stricter settings for all other Powershell sessions.

  1. Open a command prompt.
  2. Type PowerShell -ExecutionPolicy Bypass and hit Enter.
    Powershell loads inside the cmd with lowered security.
  3. Navigate to the path of your script and run it.
  4. Once you close the Powershell window, the Bypass is closed with it.

Run the script in Powershell ISE (or Visual Studio Code)

This is not really running the script. Instead, I would call it the perfect work-around to bypass your strict (company’s) execution policy :)
In Powershell ISE, it’s possible to open a script. You are then able to view the code in the editor. Instead of running the ps1, do the following:

  1. select all code in the script.
  2. hit F8 to run the selected code.

This way you don’t need extra permissions to get your work done.

powershell ise run selection

View current ExecutionPolicy level

To view your current ExecutionPolicy, system-wide, type:
Get-ExecutionPolicy

But there are more ExecutionPolicy scopes to view. Type:
Get-ExecutionPolicy -list

This shows you:

  • MachinePolicy (defined by your system administrator).
  • UserPolicy (also defined by your system administrator)
  • Process (Bypass, when run as PowerShell -ExecutionPolicy Bypass)
  • CurrentUser (when Executionpolicy was set with -Scope CurrentUser)
  • LocalMachine (when Executionpolicy was set without extra arguments)

Back to 2008

In 2008, I wrote a similar blog post about the execution policy in Powershell. In Powershell v2, the error code was different:
File cannot be loaded because the execution of scripts is disabled on this system error
Powershell v3 and later had new capabilities. And changed the error to File cannot be loaded because running scripts is disabled on this system.

Leave a Reply

avatar
  Subscribe  
Notify of