PowerShell Error: Script cannot be loaded because running scripts is disabled on this system


I wrote a small simple PowerShell script to copy (sync) files from one folder to another in Windows PowerShell ISE. It ran fine whist I was testing it running each command line individually. I then saved the file with a .ps1 extension and when I ran it I got the following error message:

File <file location and name> 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.

+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException

+FullyQualifiedErrorId : UnauthorizedAccess

The reason for this error is that the default policy does not allow a script to be run. Depending on your Windows version the default policy is either Restricted or RemoteSigned:

  • Windows Server default is RemoteSigned
  • Windows Clients default is Restricted

In my case here, I am running a Windows 10 machine and it was restricted. Here’s a brief explanation of each defaults:

  • RemoteSigned
    • Scripts can run but if they have not been created on the local machine (i.e. downloaded from another source) then they require a digital signature
  • Restricted
    • Scripts are not allowed to run but individual commands are allowed.

For more information about these defaults please refer to this Microsoft page:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1

How to Fix it

There are a couple of options to fixing this and I’ve provided the commands under each of the options below:

  1. Allowing all scripts to run (not so secure)
  1. Allowing all scripts to run under the current user (more secure)
  1. Allowing this specific script file to run (the most secure)

To run these commands, copy them to a new PowerShelll file and run them from there. If prompted to change the policy, click “Yes”. An example prompt is below:

Refer to the URL below for more information on the PowerShell set-executionpolicy command:

https://docs.microsoft.com/en-gb/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1

Good luck and feel free to leave a comment if you are having an issue.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts