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
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.
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_MACHINESOFTWAREMicrosoftPowerShell1ShellIdsMicrosoft.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 :
You can find the exact location by running the powershell variable
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.