All of this will be much easier to understand when you see it in action,
so let's head back to PowerShell and look at objects in the pipeline.
Now, if you haven't already done so,
if you want to follow along with these demonstrations,
you'll need the PSTeachingTools module which I talked
about and showed you in the last module,
because I'm going to run Get‑Vegetable.
We're going to demo all of these concepts with my generic
command so that you can focus on the concepts and not get
too caught up on the technology.
So I run Get‑Vegetable and I get a result.
Now, what type of object is that?
Well, let's rerun Get‑Vegetable and pipe it to Get‑Member.
So there you can see the TypeName is PSTeachingTools.PSVegetable object,
and you can see the methods and the properties.
Now,
some of the methods that PowerShell will display like Equals and GetHashCode,
PowerShell adds to all objects,
and that the more you look at this you'll start recognizing those things.
The methods that are unique to these object types,
in this case my PSVegetable object, are things like Peel and Prepare,
and then you can see the properties like Color,
CookedState, Count.
You can limit the results.
This isn't too bad because it's a small object,
but let's do Get‑Vegetable, pipe it again to Get‑Member,
and this time I'm going to tell it, hey,
only show me the properties.
When you get to stuff like process objects,
for example,
you'll want to limit the results so you're not scrolling back and forth.
So there we go, there are just the properties of my object, a little bit easier.
So, let's run Get‑Vegetable, and let's just do corn.
Now, look at that output.
So the output, you see UPC, Count, Name,
State, and Color,
but you don't necessarily see all of those property names in the
list there that you see with the properties,
right?
For example, there's no IsPeeled.
That's part of the object, but it's not part of the default display.
We're going to do more with properties in the next module.
Let's look at methods now.
So, again, we'll go back to Get‑Vegetable,
pipe to Get‑Member, and this time we'll tell it only show me Methods,
Peel, Prepare, those are the methods of this particular object.
Now,
we don't have to rely on these methods in order to
manipulate the object, look for commands, and in
PowerShell's case with my vegetable commands,
if there's a Get, well, there is a corresponding Set.
Not always, but often there is.
So, I can try, hey, I could use get command and say,
hey, is there a Set‑Vegetable, or just look for help,
and sure enough there is a Set‑Vegetable cmdlet,
and you can see the parameters there on how to use it.
So let's go ahead and use it.
Let's do Set‑Vegetable,
and we'll use the name for corn, we'll set the cookingstate to grilled,
and because this has ‑Whatif, and we talked about this a little bit last time,
let's do ‑Whatif.
So ‑Whatif tells PowerShell, I'm not going to make the change,
but this is what I would have done if you let me do it, and it would have
set the cookingstate to grilled on the vegetable corn.
So I'm going to retype this command,
and let's go ahead and actually do it without ‑Whatif.
Did it change?
Well, I don't know.
I didn't get a result.
Why did I not get a result?
Well, if you look back up in the help, see that ‑Passthru?
Yeah, if you see that,
that's your clue this command is not going to write an
object to the pipeline by default.
I need to use ‑Passthru if I want to force it to show me
the object that it is working with.
And we can try this again here.
Let's do Get‑Vegetable,
and now you can see that it actually did change the state,
because now it is showing up as Grilled.
And if we look at help for Set‑Vegetable,
and we can see there is a ‑Passthru parameter,
and it tells you there in the help, Write object to the pipeline.
Let's try this here.
Let's do Get‑Vegetable, and let's just get the root vegetables.
We're going to pipe all of those vegetables to Set‑Vegetable,
and change the CookingState to Roasted, we're going to change the Count to 5,
and now I'll use ‑Passthru.
See the difference?
So now ‑Passthru gives me the object that Set‑Vegetable worked on,
and you can see that it changed the Count and it
changed the CookingState of that object.
There's no scripting there, I just told PowerShell get these things,
do this with it, show me the results.
No scripting, PowerShell did that all at once.
I did it for, you know, four objects here.
It could have been 4,000.
It's the same command.
This is what I'm talking about when I get excited
about PowerShell managing at scale.
Let's try one more example here with the PowerShell pipeline.
I have a text file in my work folder called services.txt.
It's just a list of services.
Now I'm going to create an audit trail.
I want to get the services, restart them, and save the results to a text file.
In order to show you how this all works, let's break it down into simple steps.
So Get‑Service has a Name parameter, which we have seen.
And we can look at help here and see that it does accept pipeline input.
Good.
And I can test this very easily, just take a simple service like Lanmanserver,
pipe to Get‑Service, and sure enough, it works.
What I want you to do as I walk through this process is see if
you can visualize what PowerShell is doing.
Really important skill, I think,
that will make PowerShell easier if you can do it.
So we're going to do Get‑Content, the command I just ran.
So I'm going to get the content.
You already saw what it's going to do, right?
It's going to give me a list of service names.
I'm then going to pipe that, and when this happens,
by the way, when you get that little double arrow there,
that's PowerShell's way of telling you, hey,
there must be more to the command,
because I can't figure out what you want me to do.
So if you're running a command and you aren't expecting to see this,
you're thinking, hey, how come you're not running,
you probably forgot closing curly brace or parenthesis or some piece of
punctuation PowerShell doesn't understand what you are doing.
In my case,
I intentionally wanted to start each command on its own line to
try to reinforce what the pipeline is doing.
So we're going to get the content,
we're then going to pipe it to another command,
Get‑Service.
We already know what that's going to do, right?
It's going to take his output and pipe it to another command.
We're going to pipe to Restart‑Service, and I'm using ‑Passthru.
That's your clue, remember,
that that command does not write objects to the pipeline.
But I need objects to the pipeline, because if I don't use ‑Passthru,
this next line where I'm going to create the file won't do anything.
It'll create an empty file.
Using ‑Passthru tells PowerShell, take the service objects that you restarted,
send them onto the pipeline, and in this case,
send them to Out‑File, which does its thing and creates the text file.
PowerShell is doing its thing, I got no errors, so it must have worked.
And if I look at the content of my restart.txt,
there are the service objects that Restart‑Service wrote to the pipeline,
which I captured with Out‑File.
When you're learning and working with PowerShell
and just kind of getting started, don't try to write,
you know, this entire command that I just showed you.
Don't try to write it all at once.
That's what I was saying, you may think you know what you're doing,
but a lot of beginners, there's a lot you're learning,
and so if it doesn't work, you wont know,
okay, which part of the command didn't work?
Do the first part.
Good, it works.
Pipe onto the next part.
Good, that worked.
Then pipe onto the next part.
Good, that works.
As you slowly build experience, you'll be able to do more and more faster,
but don't push yourself from the beginning.
Try to visualize, what is PowerShell trying to do?
Let me point out that PowerShell doesn't mean you have to write
everything in a single pipeline expression,
but taking advantage of the pipeline means that you can get a lot
done with less work than the way we used to have to script with
tools like VBScript or batch files.
Of course,
the best way that you can understand all of this is to try it yourself.
If you make a mistake, pause,
take a moment to read the error message, it most likely will tell you
what to do to fix the problem, and then try again.
I also encourage you to visualize or verbalize what
you're trying to have PowerShell do.
Often, when you say it out loud, it becomes easier to figure out what to do,
and once you say it out loud,
then it becomes easier to figure out what commands to use.
And don't be afraid to reread help and examples.
You need to get in the habit of reading and rereading help,
even for commands that you think you understand.
PowerShell is still under active development,
and I really don't expect that to change any time soon.
You're going to see new releases a few times a year,
and the PowerShell team might introduce a new cmdlet or parameter,
or they might update help with new examples.
And don't forget the about help topic.
Staying on top of the help will go a long way in helping you get the
most out of working with objects in the pipeline. In the next module, we'll take this concept another step, so I'll see you there.