From the course: Learning PowerShell Core

The pipeline - PowerShell Tutorial

From the course: Learning PowerShell Core

Start my 1-month free trial

The pipeline

- [Instructor] The concept of objects in programming languages is a deep subject. We don't have time to cover it all in depth here. Lucky for you, though, you don't have to know the ins and out of classes and hairpins and polymorphism. Instead, you just need to understand them at a high level, especially properties. When you're first starting out with PowerShell, you'll read properties on objects quite a bit. A property is simply an attribute of an object, much like a car. A property can be how fast the car can go, how many cylinders it has in its engine, its color, et cetera. Methods, on the other hand, are actions an object can take. Using the car example, it's moving forward, moving backward, turning left, turning right, et cetera. The PowerShell pipeline is like an assembly line for objects. A PowerShell command returns some kind of object, which can then be passed directly into the input of another command. PowerShell has an ingenious system of figuring out just how to stitch these two commands together. The PowerShell pipeline is so powerful because these are objects, not just simple text strings that you may have been using in Bash or DOS and that sort of thing. This allows you to pass lots of information along the pipeline which can turn it into some really cool PowerShell script that's a whole lot easier to write than a Bash, Perl, or shoot or even Batch files. If this isn't sinking in yet, just give me a few minutes of your time in the next demo and I promise you, you'll be geeking out on this stuff as much as I do. To understand objects, let's go through a common scenario. Let's say that I want to find a Windows service, I'm just going to choose the Windows update service at random. I can find the Windows Update service by running the Get Service command. Notice here that I'm running Get Service providing the display name parameter of Windows Update and I'm assigning that to a variable. I can see the contents of that variable by outputting it like this. So notice that it is stopped right now. The service variable is a object. I can see that by piping this to a command called Get Member. Get Member is a real useful command that I highly recommend checking out because it allows you to see all of the different methods and properties of every single object in PowerShell. You really get used to this and now you can kind of dive in and figure out what's going on here. So notice that I have quite a bit of different methods and properties and things that I can work with with this service object. However, whenever I just output the service object, notice that it's only showing status name and display name. Status name and display name are properties on that object. However, you just saw earlier that I have many different properties that were returned from Get Member. I can see all of the different properties on here by piping this to select dash object, providing the property parameter and then a star to return all upping. PowerShell has a formatting system that obscures some of the methods and properties and doesn't allow you to see those things. If you pipe everything to select object and with a property star, you can see everything that way. So this has nothing to do with a pipeline yet but you will see where this fits into. So let's say that I want to, I don't know, stop the service. I can stop the service with stop dash service. And now it should be stopped. Let's try it out and see. Now you can see that it is stopped, I think it was stopped before, actually. Let's just go ahead and start it, just to make sure. So we can use start service, run it again, and now it is ready. All right, so I can manipulate the service. Let's stop it again. Check it out, okay, now it's stopped again. So this is the non-pipeline way to do this. I'm providing a parameter, a display name parameter, using the Windows Update parameter value to the display name parameter. However, I can use the pipeline to do this. So whenever I run Get Service, I showed you that it returns an object. Instead of running stop service in a different command, I can simply use a pipe symbol here and do stop dash service, hit enter, and it will do the same thing. It passes that service object directly to the stop service command. Now, let's just see what the status is. It is stopped, okay. We can do the same thing for start service as well. So you can see that using the pipeline, it passes that service object all the way through the pipeline. So we can even do something like this, we can do pass through, which will pass that same object. And then we can stop it again. You can add as many of these as you want. So I got it, started it, and stopped it again. We can add as many of these things as you want. So this is the power of the pipeline. The pipeline allows you to stitch together the output of one command to the input of another.

Contents