I just stumbled upon an interesting Technet blog for Windows scripts here.
Attempting to run a Powershell script however generated the following error:
Import-module : File C:\Scripts\CreateWindowsTile\CreateWindowsTile.psm1 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.
The link provides plenty of information – so this is a precis of what you need to know, as well as the change to make to run Powershell scripts.
First let’s take a look at the Powershell execution policies – Restricted is the default policy and it prevents the running of scripts.
Our other alternatives are:
- AllSigned – Scripts can run but must be signed by a trusted publisher
- RemoteSigned – Scripts can run. Trusted publisher scripts must be signed. Scripts that you have written yourself do not need to be digitally signed.
- Unrestricted – Unsigned scripts can be run and the user is warned before running downloaded scripts.
- Bypass – nothing is blocked and there are no warnings or prompts.
For full disclosure on these policies review the Execution Policies link above.
Choose your alternative Execution Policy carefully – Restricted is the only policy that can save you from running malicious code (signed or unsigned) on your system!
Next we need to be aware of the Execution Policy scopes:
- Process – only affects the current Powershell process.
- CurrentUser – affects only the current user.
- LocalMachine – affects all users on the computer.
So now let’s open up Powershell and review our current Execution Policy. From the Start screen just type Powershell and click on it in the search results.
Enter the following command to review the current policy:
As you can see my default policy is Undefined (which is the same thing as Restricted):
So now we can change the Execution Policy for the CurrentUser Scope to RemoteSigned. If you wish to choose a different scope and/or policy then be my guest:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
You will see the following warning – press Enter to continue:
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 at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
We can now check and see that the policy has been changed:
To finally run the Powershell script I had to run Powershell as an Administrator (right click and Run As Administrator).
Now enjoy your Powershell!