Recently when I tried to execute powershell script files to a production environment I got this error: “cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies”. In this below post we are going to cover what needs to be done to resolve this issue.
There are situations when you try to execute PowerShell script and it will give the below error message to you:-
+ FullyQualifiedErrorId : UnauthorizedAccess PS D:\> & "D:\Myfirstscript.ps1" & : File D:\Myfirstscript.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:3 + & "D:\Myfirstscript.ps1" + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
The reason behind for the above error message is a security setting created into Windows PowerShell called Execution Policy.The execution policy decides how PowerShell runs the script. By default, PowerShell execution policy is restricted; it means that script will not run.
You have to check and verify the execution policy setting by using the Get-ExecutionPolicy PowerShell command as shown below :-
get-executionpolicy
we can change the PowerShell script execution by using “Set-ExecutionPolicy”. The “Set-ExecutionPolicy” cmdlet enables the databases which Windows PowerShell scripts will be allowed to run PowerShell command on our computer.
There are four different execution policies available in Windows PowerShell:-
- Restricted – If execution policies is “Restricted” then we can not execute the scripts. Windows PowerShell can be used only in interactive mode.
- AllSigned – If execution policies is “AllSigned” then we can execute Only scripts signed by a trusted publisher can be run.
- RemoteSigned – If execution policies is “RemoteSigned” then downloaded scripts must be signed by a trusted publisher before they can be run.
- Unrestricted – If execution policies is “Unrestricted” then no restrictions; all scripts we can run.
To get more information on “Set-ExecutionPolicy”, kindly type “Get-Help Set-ExecutionPolicy” as shown below:-
get-help set-executionpolicy
We can execute Set-ExecutionPolicy command, only if we have administrator permission and for Windows Vista and Windows Server 2008 and later versions we have to open the PowerShell command prompt with Run As Administrator.Otherwise you will get the below error.
set-executionpolicy unrestricted
We can set the execution policy as per our requirement like below.
set-executionpolicy unrestricted get-executionpolicy OR set-executionpolicy remotesigned get-executionpolicy