Looking ahead in your PowerShell future,
there are other ways that you will be able to use script blocks.
We're not covering them in this course.
There are other courses in the Pluralsight library you'll find
that will explain some of these features,
but I wanted to give you a taste of what they look like.
PowerShell has the ability to run commands in the background using a job system.
The easy way to use this is Start‑Job, and you can specify a script block.
The script block, if you need to, can also include parameters.
So here, I am basically running my same Get‑WinEvent command in the script block,
but I'm also grouping the results, and sorting results,
and using the ArgumentList for Start‑Job to pass some
parameters to the script block, which then runs in the background.
When the job is finished,
I can use Receive‑Job and retrieve the information and then format,
in this case, the results as a table.
This is just another example of how script blocks are used often in PowerShell.
And when you get to scripting,
a function really is nothing more than a named script block.
So the script block here is the same script block that I used in my job.
The time, I've given it a name, Get‑LogInfo,
and turned it actually into a function.
Once this function is loaded into my PowerShell session,
I can just run GetLogInfo, and the command will run.
Script blocks are everywhere in PowerShell.
Once again, let's summarize what we've covered in this brief module.
Script blocks are used often and everywhere in PowerShell,
either in cmdlets, functions,
or things that you will write just to use ad hoc at the console.
They're treated as units of code,
makes it easier to rerun a block of code by just referencing a variable,
or when you get scripting a function, Script blocks can use parameters,
just remember that they are positional.
Script blocks are no different than any other command in PowerShell.
They write objects to the pipeline, and you'll want to write script blocks that,
in fact, will do that.
You can create your own for ad hoc work.
I do this all the time.
If I'm running something and I want to, hey,
and I need, I'm going to need to rerun this later in the day,
I'll just create a little quick script block, and
then I can rerun that script block.
If it's something that I realize, you know what, I'm
going to need this all the time, then I can start turning that into a function.
Don't focus on just the fact that, oh,
that's a script block, that's a script block, just
recognize what a script block is,
but don't get hung up on thinking, I have to write a script block. You don't.
I just took time in this particular module to demonstrate and define what a
script block is so that when you see that term,
you'll know what PowerShell is talking about. And as a reminder,
all of the code samples that I used in the slides are available
as part of the course downloads,
so you can load them up in your PowerShell editor and try them out for yourself.
We've reached the end of another module. So again,
time for you to stretch your legs,
get some fresh air, and come back, and we will continue on learning more about the PowerShell language.