So let's talk about the steps to creating a PowerShell script.
The first thing is to determine the editor that we wish to utilize.
Now, all through the course,
we've been using the Windows PowerShell terminal and console,
so that is always the first choice.
You've got the Windows PowerShell console.
It's on Windows.
You can just open it and use it.
We also have the Windows Terminal, which we've been using,
which is the new version that's available to us.
And then, of course, we also have Visual Studio Code,
which I'm quite fond of,
an application that isn't really necessarily used for creating PowerShell
necessarily, but it gives a richer interface to building them.
Now when we're looking at building the scripts,
we have a structure that we need to look at building.
So first off, you define any required modules or snap‑ins.
So, for example, if I was working with server architecture on‑premises,
for example, like Active Directory,
I would have to import the Active Directory module
as part of my PowerShell script.
Then, of course,
we declare any variables. So these are globalized variables that need values,
whether they need user input or not. And then, of course,
you define the functions,
which will be the reusable blocks that we're going to
execute throughout the process.
So to create a PowerShell script,
we first launch the selected editor of choice, so
the console or Visual Studio, etc.
You then add the PowerShell code that you want to build.
Then you digitally sign if you're distributing it or you need better security.
You save the file as a something .ps1, and that becomes your PowerShell file.
Now one of the most important things is to let either
other users that may be using the PowerShell script or
even to remind yourself. Trust me,
it's really important sometimes when you look back at PowerShell scripts that
you've written that you often wonder what they're doing.
It's good to comment them. So we have a couple of
different ways of commenting the code.
We can either do what's called single‑line PowerShell comments.
So these normally begin with the number or hash character, and
everything is on the same line, and basically, it gets ignored by PowerShell.
The next one would be block comments or multiline comments.
So you prefix this with an open and closing, so it's the chevron with
a hash and then a hash with a chevron, and then you can write
multiline comments into the PowerShell script.
You also can then create what's called comment‑based help.
So this is a collection of keywords and string values that get wrapped
within a block comment, so using the same kind of chevron hash, but
it's one that's defined out of the box,
and you can add specific property values so that when
someone types in your function name,
they can do ‑Help, and it will return those values automatically.
So what does commenting code look like?
Well,
if we look at the single line commenting, you can see I've got
a standard function called Invoke‑Message,
and it's going to write a simple message. To comment
it, you can see the green line here,
so # This function returns a simple string. That's literally the
single line commenting. Block commenting obviously uses the chevron
and the hashes to create a multiline version.
So once again,
I've got my same Invoke‑Message function, except this time it writes it in red.
So I have the same text, This function returns a simple string.
Next line, The string will be displayed in red. So it means
that when someone reads the PowerShell, they know exactly what's going on.
Then, of course, we can also comment out existing code.
So there's my function.
Let's say I don't want to use that anymore.
I can simply put the hash or the number symbol right at the beginning of
where it says function and any of the lines underneath,
and it will change that to be commented out.
So there are times when you're building a PowerShell script where you
create a function, you don't want to use it anymore,
and you want to make sure it's not going to execute. You can simply commentate that block of code.