So let's look at how we set execution policies.
There are two different ways of setting an execution policy.
The first is to use the PowerShell command called
Set‑ExecutionPolicy and the second option is to use the
ExecutionPolicy property when launching a PowerShell session.
So, for example,
if we were to execute pwsh, which is the executable for
PowerShell, we can pass that ExecutionPolicy.
Now, if we use the Set‑ExecutionPolicy,
it becomes effective immediately and PowerShell does not need to be restarted.
So it's a nice thing that we can simply execute, and change, and set it
again, and we don't need to keep closing the windows out.
Now the Set‑ExecutionPolicy command allows for scopes to be set, for example,
we talked about LocalMachine or CurrentUser, which will save it to the registry.
We can also set the execution policy for the Process scope
so it doesn't save it to the registry.
When we set the execution policy,
it may not change the current settings because of another policy assignment,
so it's always important to view the current execution policy and the
presedence order before we choose to make adjustments.
So how do we set an execution policy and scope?
It's as simple as saying, Set‑ExecutionPolicy,
passing the ExecutionPolicy option such as RemoteSigned or Unrestricted.
If we want to scope this,
then we can add a Scope property and then set the Scope,
such as CurrentUser, LocalMachine.
If we want to set it for the specific process or single session and we're
launching PowerShell, then we can use this option where we say pwsh.exe and
then pass whatever the execution policy would be.
Now once we've set an execution policy,
there may be times where you need to remove the execution policy.
So how do we remove one?
We simply rerun the ExecutionPolicy command, but this time we use a
keyword called Undefined. Undefined will revoke or remove the current
execution policy and set it to, effectively, a default value. We can do
the same for a scope option as well.
So, for example, if we wanted to reset the CurrentUser scope,
we can use Undefined on the Execution Policy.
And the same principle works for when we launch PowerShell as well,
so we can say pwsh and then set it as Undefined.