So let's go back into the console and onto my Windows machine,
and we'll execute a script directly in the PowerShell console first.
We'll then execute a script within the PowerShell ISE, so the integrated studio.
We'll then execute a script within the Windows terminal.
And then, lastly,
we'll look at how we execute a script within Visual Studio Code.
Now when it comes to creating PowerShell scripts or even executing them,
we have a few different options that are available to us.
So I'm going to close my terminal down, and we'll launch that again.
What I'm going to do first is click the Start menu and just type the word
PowerShell and literally just go to the Windows PowerShell option,
right‑click and choose administrator.
And this will take me to the actual PowerShell one that's out by default.
Now, of course, you can see it's pretty glaring colors here,
so let's just go to Colors, and I'll change the color to be a bit darker blue.
There we go, a bit nicer.
Now this is the standard Windows PowerShell interface
that's available on all Windows machines.
Really straightforward.
You launch it, and you can do all the standard.
So if I do Get‑ChildItem, for example, and just execute,
it'll give me a list of files.
It is color‑coded, so you can see it's fairly straightforward.
If I want to change directory, let's say I want to go to \Users\Trainer\Data\,
then I can change directory.
And if I wanted to execute a PowerShell script,
I'm just going to say .\Script.ps1.
And sure enough, it just executes the script automatically.
So really, really straightforward.
This is the most common way to execute scripts.
You just launch that Windows PowerShell console.
An actual fact, when you use other applications,
for example, server products like SharePoint or even Exchange,
when you launch their version of the PowerShell console,
it's actually just built on top of the regular Windows one that's there.
Okay, so let's just exit that one.
The second option available to us, I'm going to type PowerShell again,
is the one that's called PowerShell ISE.
And we've touched on this in the past,
so I'm going to launch this again and wait for that to load.
Now, of course, I've got some extra pieces here,
so I'm just going to drag this to the right.
And what you'll see from here is we get a canvas at the top for typing.
So let's say I was going to say Get‑ChildItem again.
I can select it, and execute, and it shows the results in the bottom.
At any point, of course,
if I'm not sure what modules I have loaded or I want to
know how to do something or I'm not sure,
let's say I wasn't sure what Get‑ChildItem actually does,
I can actually search for it here, click Show Details,
and then it will list me all of the properties that are part of that function.
It also gives me the ability to kind of,
if I just type some values here, for example,
just arbitrary, I can say Insert,
and it will populate that into the bottom section.
I can also copy that and paste it somewhere else, too.
So it's a nice interface.
I really like using Windows PowerShell ISE just because it's a clean interface,
you can quickly see the code, you can execute,
you can do all kinds of things with it as you need to.
Now,
the only downside here is that we're not sure exactly how long
this is going to last because it's not really been invested in
as an application on the machines.
So, what do we look at replacing that with?
Well, first off,
you've noticed that we've been using what's called the Windows Terminal,
so I'm going to right‑click and do Run as administrator.
This is the old sing and new dancing kind of interface.
What's nice about this one is that you can kind of have the ability to
flick different versions of tabs between each other.
So for the predominantly, we've been using PowerShell 7,
but of course, I did flick to Windows PowerShell to look at signing,
but I could also go to a command prompt or Azure Cloud Shell or
any other number of things that we wish to add.
So if I choose PowerShell 7, I get the PowerShell 7 option here,
and I can then simply execute.
So if I say cd and go into the Data folder,
if I wanted to execute that script that's in there,
same syntax again.
Now you notice it comes back.
It'll say, okay, unfortunately,
it can't be loaded because running scripts has been disabled for PowerShell 7.
So remember how we do this?
Set‑ExecutionPolicy Unrestricted, and it comes back and it says,
but the setting is overridden by another policy.
So then we have to say, well, Get‑ExecutionPolicy,
and we can see it's been defined as Restricted.
So, but we know how to go through all of this,
and we can change it as we need to.
But you can run the scripts in exactly the same way by just executing them.
Now, of course, mine's blocked here for that very reason.
If I go back to the administrator one for Windows PowerShell,
you can see that if I type Script on here,
I can execute that script.
So PowerShell 7 does have some extra security capabilities,
which we already talked about earlier.
Okay, so that's one option.
But, when we were looking at building functions,
it's not the easiest to be able to write a function,
you know, to kind of go in and then say,
Write‑Host, and then a message, and then do something else,
and then close it off.
And it kind of isn't the best interface for writing,
which is why I really like using PowerShell ISE.
Because if we were to do function Test, and do so,
and then say Write‑Host, you can see I get IntelliSense as well.
And I can say ‑ForegroundColor Green, and then I can close.
Then I can execute the function here.
And once I get underneath it, I can say Test, and boom, it works.
And it's nice because it gives me a structured view.
So what's our replacement, then?
So clearly, it's not the Windows terminal.
So what we're going to talk about is Visual Studio Code.
So I'm going to right‑click here, choose Run as administrator.
You can download this.
It's actually a developer tool that's available for us to do things.
I'm actually just going to drag this over a little bit here,
and just close that,
and get rid of that bottom section so I can show you what happens.
Now, what we've got here is a code editor that's available to us.
What I can do now is say File, New File,
and let's go ahead and create a function called Show,
let me just type Show‑Message,
and we'll do very simple Write‑Host "This is a message" like so,
and we'll say ‑‑‑ let me do ‑‑‑ Okay, so now we have that.
I can say File, Save As, and then it's going to ask me where I wish to save.
So I'm going to go to my Data folder, and I'll call this function.ps1.
And I can go and change it in here, so you can see the list of options,
so PowerShell.
So function, I'll save it as that.
And once that's done, what I can then do is go onto the section here,
right‑click, and say Run Selection.
And then, of course,
what it will do is tell me that my PowerShell is actually starting up.
So I can say switch to a different session or restart the current session.
So I'm going to do that.
So you can see, let me just say A for always run.
Okay, let me get rid of the bottom section,
and then I can select that there, and then say Run Selection.
Okay, so it's now created that.
I can then say Show‑Message, and it will render the message for me.
So, very similar to the PowerShell ISE,
except what I can do is I have this rich interface of
where I can kind of run a single line, check a specific variable.
I can pass values backwards and forwards.
I can also, then see any problems that may have occurred in the code itself.
So, for example, if I had a variable here and I had some values inside of it,
and then I wanted to kind of get that variable,
pipe it out, and then say Select, you know,
the first one, for example, then what it does when it starts to check the file,
it will actually look at that one, and IntelliSense will run.
So I'm going to put it back to the output in the debug console,
for example, or the terminal.
I could then leave that to run,
and PowerShell would automatically identify a problem.
So, for example, it will automatically say you can't have the word Select there.
It should be Select object, for example.
So, it's a nice, powerful interface for actually writing and executing scripts.
Now if I wanted to execute the specific script that we've got,
I can say Open File, and then I can open the script that I've written before.
You'll see it's here.
I can then simply just go here and say Run, and this will run that for me.
Now, of course, it comes back and complains because of security.
And why does it complain?
Because it has a signature associated to it.
But if I wanted to run the individual piece,
I can run that and then run Write‑Message here,
and then I'm able to execute it as you would normally.
So the signature is the bit that causes a security issue,
but that's perfectly fine.
But, as you can see, it straightforward.
It lets you kind of run the PowerShell script directly in the top section to the bottom section.