How to troubleshoot error: "PowerShell Error - The file is not digitally signed"

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.

Issue Description:
Generally, this error comes when trying to run a PowerShell script that has not been signed by a trusted publisher.

Error:
image.png


Resolution :
There are different methods to overcome this error. The options are 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, check the execution policy using the cmdlet Get-ExecutionPolicy
  • The list parameter in Get-ExecutionPolicy cmdlet shows the execution policy for each scope.
The default execution policy for all Windows versions 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 an unsecured 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 changed again.

Solution 2: Changing the Execution Policy Temporarily
Instead of changing the execution policy permanently, 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 an 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 the contents of the script are safe then, there is the option to unblock it to run on the session using the Unblock-File cmdlet
  • Once the Execution policy is changed permanently or temporarily for a session or a particular script, continue to run the script but make sure the contents of the script do not harm the computer.