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:
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:
- Allowing all scripts to run (not so secure)
1 2 3 |
Set-ExecutionPolicy RemoteSigned |
- Allowing all scripts to run under the current user (more secure)
1 2 3 |
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser |
- Allowing this specific script file to run (the most secure)
1 2 3 |
Set-ExecutionPolicy ByPass -File <File Name>.ps1 |
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:
Good luck and feel free to leave a comment if you are having an issue.