So now that we've looked at the precedents,
how do we actually go about setting execution policies?
Well, to set execution policies, they can be assigned, as we've
talked about, to a default scope or a specific scope.
The default scope will always be LocalMachine,
which will actually affect everybody who goes into that computer. Execution
policies can also be used on a per PowerShell session.
So as we launch the PowerShell client,
we can append what that policy should be to the Start command.
So how do we set the default execution policy? Well, just like the
previous one, to retrieve the current policy, remember,
we retrieved the scopes and the policies last time by using
Get‑ExecutionPolicy with the parameter of ‑List.
This time around, if we just call Get‑ExecutionPolicy,
it will retrieve the current applied policy.
We can then set the execution policy by simply calling
Set‑ExecutionPolicy and then passing in whatever that would be.
So in this instance, I'm setting it to Unrestricted,
which actually is one of the most common levels that you would set it to.
So what about if we wanted to set scoped execution policy? Now think of this.
In the last example, I said Set‑ExecutionPolicy Unrestricted,
which means it will be applied to everything.
But we can go through and set that to those specific scopes.
So if you remember,
we have Process, CurrentUser, and LocalMachine, and then we have our policy.
So in this example here, I'm setting the ExecutionPolicy
to RemoteSigned for the LocalMachine,
which means that will affect everybody who comes into that machine.
The next example here would be, well, actually,
for the current user, I want to allow AllSigned, so a different type of policy.
It's not going to affect everybody.
It will just be the currently logged in user.
And then, of course, we can do this remotely as well.
So in previous PowerShell courses that have been written
and blog posts that have been written, we can learn about Invoke‑Command.
Now the Invoke‑Command is used heavily to run a command on a remote machine.
So, for example, if I need to execute a policy somewhere on a different machine,
I could say Invoke‑Command.
Then, of course, we have the parameter called ComputerName,
which will be the name of the remote machine.
And then the parameter called ScriptBlock lets me
pass in whatever that would be.
And it's as simple as saying Get‑ExecutionPolicy, and
then I can set the execution policy.
Right now it's blank, but I could set that to a specific value.
Now, how do we set the execution policy for a single session?
Let's say you you don't want to have something done by Group Policy.
Or maybe you do, but for this specific PowerShell session,
you want it to be different.
Well, the first thing we do is we can launch PowerShell using the command line,
which is pwsh.exe.
We can actually just launch that from the command line.
What we do is we utilize an execution parameter.
So the parameter is ‑ ExecutionPolicy,
and then we assign the policy, whether it's Unrestricted,
Restricted, or AllSigned.
This will not save this value to the registry.
This is a one‑time execution value for that specific session.
So if you tie all that together, you're going to type pwsh, space, ‑ ExecutionPolicy, and then the policy.