Execution policy error

There are instance when you execute your PowerShell scripts on server from your command prompt you will end up seeing below mentioned error message.

Error
Message

File cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details

This article mentions the steps which you need to follow to fix “File cannot be loaded because the execution of scripts is disabled on this system.” error message. Let’s learn how to set executionpolicy in PowerShell.

The primary reason why you end up seeing this error message is because of the PowerShell Execution Policy Security Settings which are built into the Windows PowerShell. It basically decides how PowerShell will execute the scripts.

The Windows PowerShell execution policy is by default set to be RESTRICTED and hence the scripts will not run until the settings are changed.

How
to Verify the Existing PowerShell Execution Policy Setting

Step 1: Open Command Prompt and Enter PowerShell

Step 2: Type Get-ExecutionPolicy and hit enter as shown in the snippet below.

Get-ExecutionPolicy
How to Verify the Existing PowerShell Execution Policy Setting

There are basically Four Different Windows PowerShell Execution Policy behaviours namely:

  • Restricted: – It is primarily an interactive mode, wherein no PowerShell scripts can be executed
  • Unrestricted: – As the name suggests there is no restriction set and henceforth all the PowerShell scripts can be executed.
  • RemoteSigned: – As the name suggests all the downloaded scripts needs to be signed from a trusted published to execute them on the server.
  • AllSigned: – It means that the scripts which are signed from a trusted publisher can only be executed

Since in our case we have seen that Windows PowerShell Execution Policy is set to be Restricted on the server. We need to change the behaviour and for that we need to use the PowerShell cmdlet Set-ExecutionPolicy.

about_Execution_Policies

PowerShell’s execution policy is a safety feature that helps to control the conditions in which PowerShell loads configuration files and runs scripts. One of the primary advantages of PowerShell’s execution policy feature is that it helps prevent malicious scripts’ execution.

PowerShell execution policies

The PowerShell execution policies are AllSigned, ByPass, Default, RemoteSigned, Restricted, Undefined, and Unrestricted.

Execution policy scope

One can set an effective execution policy that is only valid for a particular scope. The available valid values for Scope are MachinePolicy, UserPolicy, Process, CurrentUser, and LocalMachine. When setting an execution policy, the default value is LocalMachine.

Privileges Needed to Execute Set-ExecutionPolicy PowerShell cmdlet | Privileges Needed to Set Executionpolicy PowerShell

One must be having Windows Administrator Privileges to execute Set-ExecutionPolicy PowerShell cmdlet on your windows operating system. If you are an administrator then you can open Command Prompt with Administrator Privileges (Run as Administrator) to execute Set-ExecutionPolicy PowerShell cmdlet. Either of the below options will work fine for you.

How to Change PowerShell Execution Policy Setting to Unrestricted | How to Set ExecutionPolicy Unrestricted

Step 1: Open Command Prompt and Enter PowerShell

Step 2: Type Set-ExecutionPolicy Unrestricted and hit enter as shown in the snippet below.

Set-ExecutionPolicy Unrestricted 
How to Change PowerShell Execution Policy Setting to Unrestricted

How to Change PowerShell Execution Policy Setting to RemoteSigned | How to Set Executionpolicy RemoteSigned

Step 1: Open Command Prompt and Enter PowerShell

Step 2: Type Set-ExecutionPolicy RemoteSigned and hit enter as shown in the snippet below.

Set-ExecutionPolicy RemoteSigned
How to Change PowerShell Execution Policy Setting to RemoteSigned

How to Get PowerShell
cmdlet Syntax Help to understand information about PowerShell Syntax and
Acceptable Parameters?

If you want to understand the syntax and the parameter acceptable for a PowerShell cmdlet then execute Get-Help followed by PowerShell cmdlet Set-ExecutionPolicy as shown in the snippet below to displays information about PowerShell concepts and commands, including cmdlets, functions, Common Information Model (CIM) commands, workflows, providers, aliases, and scripts.

Get-Help Set-ExecutionPolicy 
How to Get PowerShell cmdlet Syntax Help to understand information about PowerShell Syntax and Acceptable Parameters

Conclusion

In this article we have seen the steps which you need to follow to fix “File cannot be loaded because the execution of scripts is disabled on this system.” error message to run PowerShell scripts in your environment.

Reference

  • About Execution Policies

Chetna Bhalla

LESS ME MORE WE

Chetna Bhalla, the founder of MyTechMantra.com, believes that by sharing knowledge and building communities, we can make this world a better place to live in. Chetna is a Graduate in Social Sciences and a Masters in Human Resources and International Business. She is an alumnus of Vignana Jyothi Institute of Management, Hyderabad, India. After graduation, Chetna founded this website, which has since then become quite a favorite in the tech world. Her vision is to make this website the favorite place for seeking information on Databases and other Information Technology areas. She believes that companies which can organize and deploy their data to frame strategies are going to have a competitive edge over others. Her interest areas include Microsoft SQL Server and overall Database Management. Apart from her work, Chetna enjoys spending time with her friends, painting, gardening, playing the violin, and spending time with her son.


Table of Contents

  • Issue
  • Solution

Issue

If your Windows PowerShell execution policy is set to ‘Restricted’, then when importing the DirSync PowerShell module you might run into the following error:

SyncdirsyncImportModules.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.

At line:1 char:1

+ import-module dirsync

+ ~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : SecurityError: (:) [Import-Module], PSSecurityException

    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

Solution

To fix the issue, please run the following command and import the DirSync module again.

 Set-ExecutionPolicy RemoteSigned -Scope process

 Execution Policy Change

The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at

http://go.microsoft.com/fwlink/?LinkID=135170.

Do you want to change the execution policy?

[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y


While running a PowerShell script, if you get an error message, cannot be loaded because running scripts is disabled on this system it is because the PowerShell execution policy is set up by default as Restricted and doesn’t allow running the script.

It throws an exception message as “cannot be loaded because running scripts is disabled on this system

running scripts is disabled on this system
running scripts is disabled on this system

PowerShell Scripts Disabled error message as below

.Get-Printers.ps1: File CLGet-Printers.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170

PowerShell has built-in security features implemented. The PowerShell Execution policy is a safety feature that controls the conditions to run scripts and load configuration. The Execution Policy in PowerShell is set up to avoid running malicious scripts in your system.

In this article, I will explain different ways to fix file cannot be loaded because running scripts is disabled on this system.

Let’s understand how to enable the execution of running PowerShell script using different execution policies.

PowerShell Execution Policy in Restricted mode avoids running script to ensure safety.

Cool Tip: Use Get-ExecutionPolicy to know the default policy applied to your system.

Using RemoteSigned Execution Policy

RemoteSigned execution policy is a more secure option. On enabled, it allows local scripts to run. All other scripts from outside require signed by a trusted publisher.

Set-ExecutionPolicy RemoteSigned 
  • Set up RemoteSigned execution policy
  • Allows to run the local script.
  • Now if you try to run .ps1 file, it won’t give you the running script is disabled on this system error.

Cool Tip: How to fix the script is not digitally signed error in PowerShell!

Using UnRestricted Execution Policy

UnRestricted is the default execution policy set up on the non-windows systems. If you enabled this policy on the system, PowerShell can run any unsigned script file.

You can change the execution policy using the Set-ExecutionPolicy cmdlet.

Set-ExecutionPolicy Unrestricted
  • Warns the administrator about the policy change and security risk associated with UnRestricted mode.
  • If you select option, Y or Yes to All, it will apply the policy.
  • After setting up the UnRestricted execution policy, if you try to run the ps1 file, it won’t give the running script is disabled on system error.
Set-ExecutionPolicy Unrestricted to enable script
Set-ExecutionPolicy Unrestricted to enable the script

Cool Tip: How to use the multiline command in PowerShell!

Using ByPass Execution Policy

Using the bypass execution policy, nothing is blocked and if you try to run a script, it won’t give your a warning or prompt for confirmation.

With the ByPass policy, scripts run temporarily with lower security.

powershell -executionpolicy ByPass -File .Get-Printers.ps1
  • runs ps1 script file with lower security and executes it.
  • Once your close the PowerShell session, it will also close ByPass with it.
ByPass - To fix running scripts disabled on this system
ByPass – To fix running scripts disabled on this system

Using PowerShell ISE

If you don’t want to set an execution policy and still want to run the script inside the ps1 file, the best workaround is to open the script file in PowerShell ISE.

Select all the code in the script and hit the F8 button to execute the script.

Note: Don’t use F5 for execution, else it will try to run a ps1 file and throw a file cannot be loaded error.

fix for running script disabled
Select Code-F8 to fix the file cannot be loaded error

This way, you won’t require any permissions or any kind of running script is disabled on this system error.

Cool Tip: How to add a newline to string or variable in PowerShell!

Conclusion

I hope the above article to fix the file cannot be loaded because running scripts is disabled on this system error using the set-executionpolicy helpful to solve your problem.

Use the RemoteSigned execution policy to avoid running any malicious scripts or if a script is local, open the PowerShell script, select all code, and hit F8 to execute it.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

On trying to run a PowerShell script from the PowerShell console, I received this error message: “File C:tempGenerateRpt.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.”

In some cases, it’s a “The file is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies.” error.

cannot be loaded because running scripts is disabled on this system

Root Cause:

This is due to the Windows PowerShell execution policy being set to prevent untrusted scripts which can affect your Windows client environment. The default execution policy is “strict” on client operating systems like Windows 10 PC.

Solution for “cannot be loaded because running scripts is disabled on this system:

How do you enable running scripts is disabled on this system error? To fix this issue, we have to set the execution policy using the Set-ExecutionPolicy cmdlet, so that the PowerShell script runs on the particular machine. Here is to permit PowerShell script execution:

  1. Open PowerShell Console by selecting “Run as Administrator” and get the execution Policy with the command: Get-ExecutionPolicy to get the current policy applied, such as “Restricted”. PowerShell Run as Administrator
  2. Set the execution Policy with the following command: Set-ExecutionPolicy RemoteSigned
  3. Type “Y” when prompted to proceed.

You can also use Set-ExecutionPolicy Unrestricted to remove all restrictions on your security policy (However, the RemoteSigned execution policy is ideal!).

The RemoteSigned policy requires scripts and configuration files from the internet must be signed by a trusted publisher. Behind the scenes, it sets the registry key: HKLMSoftwareMicrosoftPowerShell1ShellIdsMicrosoft.PowerShell . The policy parameter takes the below values:

  • Restricted – No scripting allowed
  • Unrestricted – You can run any script, No signing is required.
  • RemoteSigned – Good for Test, Dev environments. Only files from the internet need to be signed. This is the default setting on servers.
  • AllSigned  – local or remote script – It should be signed by a trusted publisher.

Fix for PowerShell Script cannot be loaded because running scripts is disabled on this system error

What if you can’t set the Execution Policy by running PowerShell as Administrator?

To set the execution policy for the current user scope, use the following:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

The default scope is “LocalMachine” which sets the policy for all users.

Bypass ExecutionPolicy Temporarily for a Session

You can bypass the execution policy just for a one-time session. In the command prompt, type: PowerShell -ExecutionPolicy Bypass

Once you close the PowerShell window, the current PowerShell session ends, and the Bypass is also closed with it. This allows you to run a PowerShell script temporarily while keeping the ExecutionPolicy settings for all other PowerShell sessions.

How about running the script in PowerShell ISE?

You can bypass the execution policy by selecting a block of the script and then hitting F8 (Run Selection).

Since I intensively started using Visual Studio Code across my devices, with PowerShell as my main terminal, I started running into the following error quite a lot:

<file> 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.

What’s happening here, is that your computer is preventing you from running potentially harmful scripts. This safety mechanism can broadly be configured on three levels: for your computer, for a user or for a particular session. These safety mechanisms are execution policies.

It’s good to know that you can bypass this safety mechanism by copying the code from a script and running it manually in a PowerShell terminal. So execution policies do not block the code, but they make sure you don’t unintentionally run malicious scripts.

However, there’s a more conventional way. You can relax the execution policies, by taking these simple steps.

  1. Press the windows-button on your keyboard.
  2. Type ‘PowerShell’
  3. Right-click Windows PowerShell
  4. Click Run as Administrator
  5. Run the following command and confirm with ‘Y’
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

We have changed the execution policies to allow local and remote signed scripts and we have done this for the scope of your machine. Conclusion: You can now run PowerShell scripts on your computer.

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Понравилась статья? Поделить с друзьями:
  • Execution finished with errors result near syntax error
  • Execution failed on sql near syntax error
  • Execution expired timeout error
  • Execution error pcre limits exceeded
  • Executing grub install dev sda failed this is fatal error