Want to save this blog for later? Download it now.

Powershell Scripts allow you to automate common tasks within Windows, Microsoft applications and of course Office 365. You can often download handy scripts written by other people off the internet for use on your own computer.

Unfortunately, these scripts could potentially do bad things that you didn’t expect so Microsoft has protected you from this by disabling Powershell scripts by default on Windows 7. Also, you really need to make sure that you only download or use Powershell scripts from a trusted source!

Anyway, if you try and run a Powershell script, you’ll get this helpful error message.

PS C:\> .\script.ps1
File C:\script.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:17
+ .\connect-ps.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

PS C:\>

Fortunately, there is an easy way to fix this. You just need to change the Execution policy on your computer using a simple Powershell command.

Set-ExecutionPolicy RemoteSigned

This will allow you to run scripts that have been written by “a trusted publisher” (Microsoft determines who is trusted and who isn’t).

You can change the RemoteSigned portion of the command above to any of the options in bold below, depending on what you need to achieve.

Restricted – No scripts can be run. Windows PowerShell can be used only in interactive mode (This is the default).
AllSigned – Only scripts signed by a trusted publisher can be run.
RemoteSigned – Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted – No restrictions; all Windows PowerShell scripts can be run.

Once you run the command, you will be asked to Confirm your action. Press Y on the keyboard and enter to confirm. You can see an example here.

PS C:\> Set-ExecutionPolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
PS C:\>

You can now run Powershell scripts!

Wait, I got an error!

Sometimes you will not have permission to set your Execution Policy and you will see an error message like this:

PS C:\> Set-ExecutionPolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

PS C:\>

On some computers, you will need Administrator access to run this command. If you are an Administrator, you can fix this by running Powershell as Administrator.

To do this, right click the Powershell icon and click Run as Administrator. Click Yes to any security warnings that come up and you should be able to run the command as normal!

Changing it back

We recommend you change the remote execution policy back to Restricted when you are done running your script. This prevents evil scripts from running on your computer accidentally. To do this simply change the execution policy back using the command.

Set-ExecutionPolicy Restricted

For more information on Execution Policies and Scripts, check out these links:
http://technet.microsoft.com/en-us/library/ee176961.aspx
http://technet.microsoft.com/en-us/library/ee176949.aspx

If you found this blog post useful, and want to refer to it again, why not download it as a PDF?

2 Comments

Leave a Comment

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