What is ExecutionPolicy?

The Power of Powershell is that you can control on how Ps1 (Powershell script) will run on a computer.

By default, you can't run a script.

What is a script?

A script is a couple of lines written in the same file and save

if you see a * in the Tab, is meen that this file not save since the last change

As you can see from this image My file not save (we have the * ) that mean this is not a script.

Only when I save my file I become a Script.

Before I save my script, I can run it

Now I save my script (no more *) and try to run I got this error

 

ExecutionPolicy

Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts

 

How can we change this?

First we need to know what execution policies you currently have.

Running this comman will show you  

Get-ExecutionPolicy

Restricted meen that no script can run on this computer.

Let's change it to RemoteSigned (All your script can run but if you download from the web the script must be sign with a certificate) 

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force

More info about ExecutionPolicy you can find in the document (link

Sometimes you get an error if you try to Set-ExecutionPolicy (becuse you not an admin on your computer) you may need to set it by the scope (Scope is the environment you control: This Proccess, The User, The Computer)

You can see the scope, with this command:

Get-ExecutionPolicy -List

 

You can see that CurrentUser Scope is Restricted, To change this scope Run this command:

 Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force