While trying a run a powershell file, getting error : "script.ps1 :File path\script.ps1 cannot be loaded. The file path\script.ps1 is not digitally signed. You cannot run this script on the current system."
The article is relevant for troubleshooting script errors and for checking execution policies.
Generally, this error comes when we try to run a PowerShell script that has not been signed by a trusted publisher.
Error Screenshot :
There are different methods to overcome this error. We may choose to either sign the PowerShell script, change the execution policy, bypass the policy or unblock the file so that it can run once on that session.
Check Execution Policy
First of all, we need to check our execution policy using the cmdlet Get-ExecutionPolicy
The list parameter in Get-ExecutionPolicy cmdlet tells us the execution policy for each scope.
The default execution policy for all windows version except for Windows 2012 R2 is Restricted. The default execution policy in Windows 2012 R2 is RemoteSigned.
Solution 1 : Changing Execution Policy Permanently
The easiest but unsecure method of getting rid of this error message is to change the ExecutionPolicy using the SetExecutionPolicy cmdlet. The following command sets the execution policy to unrestricted.
Press Y to confirm the change when prompted. The policy change is updated in the registry and will remain until we change it again.
Solution 2 : Changing Execution Policy Temporarily
Instead of changing the execution policy permanently, we could set a different policy for a single PowerShell session. This is done using the ExecutionPolicy parameter of powershell.exe
Open a command prompt or PowerShell and run the command:
The above command opens a PowerShell session with execution policy for that session set to Bypass which means nothing is blocked.
Unblocking a File that was downloaded
When the execution policy is RemoteSigned, the files that are downloaded from the internet (or from emails) are blocked to protect our running unsafe scripts. If we trust the contents of the script are safe then we can unblock it to run on our session using the Unblock-File cmdlet
Once we have changed the Execution policy permanently or temporarily for a session or a particular script we can continue to run the script but before we do that, make sure the contents of the script does not harm our computer.