So let's go on to a Windows 10 machine, which has PowerShell installed,
and we'll review the PowerShell execution policy precedence by executing
the PowerShell and looking at what's available to us.
So here we are on the Windows 10 machine,
and the first thing we want to do is actually launch PowerShell itself.
So there are a few different ways of doing this.
If we just go to the Start menu and type powershell,
you'll see we have Windows PowerShell.
I have PowerShell 7.
I have the PowerShell ISE.
And then, of course, I can also type terminal,
and I actually have Windows Terminal.
Now I like to use the Windows Terminal,
so I'm going to right‑click here and choose Run as
administrator and just click Yes.
The reason I use Windows Terminal is because it's the newest version.
It also lets me kind of set some specifics around the
types of PowerShell that are being used.
So you'll notice this was the first bit of PowerShell, and it says pscore6.
If I choose the drop‑down here, I've got the Command Prompt,
Azure Shell, and PowerShell 7.
So I'm going to choose PowerShell 7 here.
And you can see there's a new update.
I'm just going to ignore that for now and just click clear.
Okay, so now we're sitting at our PowerShell prompt.
So what we want to do here is actually look at the execution
policies and the scopes that kind of tie altogether.
So to do this, we're just going to say Get‑ExecutionPolicy.
And then we'll use the parameter of List.
Now what you can see here is it gives us the scope.
So notice we have Process, CurrentUser, and LocalMachine.
And then, of course, we have two more, which are the group policy ones,
which are MachinePolicy and UserPolicy.
Now, as you can see, the LocalMachine one has been set to RemoteSigned.
That's because I kind of set that before.
But the others are set to Undefined,
which means that if we think of the precedence of how that goes,
that if I try to execute something, then it's going to check the Process,
check the CurrentUser, and then check the LocalMachine,
and then it will determine which one it should be.
In this instance, it will pick this option here, so the RemoteSigned option.
Now I suppose the question would be, What would happen if we try to
execute a PowerShell script here? So, first off,
if I just do a directory, you can see that I have a Test.ps1 script right here,
so I'm going to clear that.
So what I want to be able to do is execute the Test.ps1 file.
So I'm going to execute this here. And notice it just returns back and says,
This is a test script. So it actually executed the command.
Now, if we go back to the list, how does that work?
Well, remember the LocalMachine policy, whether it's RemoteSigned or not,
it will always execute local scripts and commands.
So even though it's been set to RemoteSigned and should require what you would
expect a digital signature assigned to it, if it's local,
it's always going to work.
So that's kind of a practical thing to see. Now if we change that, so I'm
going to say Set‑ExecutionPolicy and just say Restricted,
and then we'll clear that out, and then we'll just go back and say
Get‑ExecutionPolicy, you can see that's been changed.
Now what does that do when we try to execute the script again?
So I'm trying to run it. Now, of course,
this time it now fails the execution of that one because for the specific
scope, we have set it to Restricted, which means nope,
it's not going to work whatsoever.
And we could continue to change the execution policy.
Now, of course,
if we look at changing those in the future to perhaps
having a CurrentUser or even a Process one,
then we can have completely different impacts and different
effects by running the same PowerShell script.
So, for example,
if we had something set at Process, CurrentUser, and LocalMachine,
depending on which one it hit first would determine what would happen.
But it's as simple as executing Get‑ExecutionPolicy ‑List to be able to see the enforced policies for the specific scopes.