Most of the existing answers explain the How, but very few explain the Why. And before you go around executing code from strangers on the Internet, especially code that disables security measures, you should understand exactly what you're doing. So here's a little more detail on this problem.
From the TechNet About Execution Policies Page:
Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.
The benefits of which, as enumerated by PowerShell Basics - Execution Policy and Code Signing, are:
- Control of Execution - Control the level of trust for executing scripts.
- Command Highjack - Prevent injection of commands in my path.
- Identity - Is the script created and signed by a developer I trust and/or a signed with a certificate from a Certificate Authority I trust.
- Integrity - Scripts cannot be modified by malware or malicious user.
To check your current execution policy, you can run Get-ExecutionPolicy
. But you're probably here because you want to change it.
To do so you'll run the Set-ExecutionPolicy
cmdlet.
You'll have two major decisions to make when updating the execution policy.
Execution Policy Type:
Restricted
† - No Script either local, remote or downloaded can be executed on the system.
AllSigned
- All script that are ran require to be digitally signed.
RemoteSigned
- All remote scripts (UNC) or downloaded need to be signed.
Unrestricted
- No signature for any type of script is required.
Scope of new Change
LocalMachine
† - The execution policy affects all users of the computer.
CurrentUser
- The execution policy affects only the current user.
Process
- The execution policy affects only the current Windows PowerShell process.
† = Default
For example: if you wanted to change the policy to RemoteSigned for just the CurrentUser, you'd run the following command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Note: In order to change the Execution policy, you must be running PowerShell As Adminstrator.
If you are in regular mode and try to change the execution policy, you'll get the following error:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option.
If you want to tighten up the internal restrictions on your own scripts that have not been downloaded from the Internet (or at least don't contain the UNC metadata), you can force the policy to only run signed sripts. To sign your own scripts, you can follow the instructions on Scott Hanselman's article on Signing PowerShell Scripts.
Note: Most people are likely to get this error whenever they open Powershell because the first thing PS tries to do when it launches is execute your user profile script that sets up your environment however you like it.
The file is typically located in:
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
You can find the exact location by running the powershell variable
$profile
If there's nothing that you care about in the profile, and don't want to fuss with your security settings, you can just delete it and powershell won't find anything that it cannot execute.
Get-ExecutionPolicy -List
. – Bacon Bits