So let me give you some basics on working with script blocks.
Now in this module, all of my code samples are going to be in the slides,
I'm not really going to be any live demo because it's just
as easy to see them on the screen here,
but the course downloads does have all of the code that I'm showing you,
so you can download that, grab the code,
open it up in your editor, and try these things out for yourself.
So I'm going to create a script block and I'm going
to save this to a variable $sb.
In the script block,
we're going to have a set of curly braces and whatever code that you want
to run in the script block goes inside the curly braces.
Inside the curly braces, you can have a single line of code as I have here,
you can have multiple lines of code,
just hit Enter after each line and then just make sure
you close your closing curly brace.
PowerShell will figure out what is inside your
script block and know how to run it.
You can have as much code as you want inside those outer curly braces.
If it's a single line like I have here, you really don't have to do anything.
If you have multiple lines of code,
just hit Enter after every line and then PowerShell will figure
it out when you try to use that script block.
To run the script block like that one I just created,
you can use the invoke operator which is the ampersand here,
so I can just tell PowerShell ampersand,
meaning run or invoke, the variable $sb because that's a script block,
PowerShell knows what to do.
So the script block runs and I get the results.
The output of the script block is the same as if I had
manually run the code within the script block.
The difference is the script block made it a whole
lot easier for me to run that code.
If I decided later in the day I want to get the same data again,
all I have to do is rerun that really short script block variable,
I don't have to try to rerun all the code that is inside the
script block that I created originally.
This obviously makes PowerShell really easy to reuse.
The other way that you can invoke a script block is to use Invoke‑Command.
Invoke‑Command, if you looked at the help,
you'll see has a script block parameter which is positional,
so I can just do Invoke‑Command or the alias icm and
give it the name of my variable, which has a script block,
and PowerShell will do its thing,
and I'll get the same result as I just showed you.
You'll also find Invoke‑Command very useful when you go to
PowerShell remoting because I'll be able to run that script block
remotely on one or more multiple computers.
So as I said earlier,
script blocks are everywhere in PowerShell used all the time,
you've seen them, you may not have known what they were called.
So Get‑Process, piping it to Where‑Object.
Where‑Object is using a filtering script block so that code within this
curly braces where $_.ws is greater equal to 100 MB,
PowerShell evaluates or runs that code, filters the process objects,
and then I can pipe them to select object and there are the results.
Here is another example.
I am first defining a hashtable called $new and this will be
parameters that are part of the new item cmdlet which is
going to be in my foreach object.
So I'm going to take the numbers 1 through 10,
and for each 1 of those numbers,
I'm going to create a new directory called Data‑1‑2‑3 and
so on and I'm splatting the hashtable.
So for each object is using a script block,
so you have the curly brace New‑Item code closing curly brace.
Don't confuse the hashtable that I've defined with $new with a script block. The hashtable has the @ symbol in front of it, the script block does not.