A Novice’s Guide to Powershell Pipelines - Pt 1

David G
Tech Jobs Academy
Published in
4 min readJul 22, 2016

A PowerShell pipeline is a sequence in which a command’s output is passed to another command. This sequence can be comprised of two or more segments (pipes). Each segment is comprised of one command (or more specifically a PowerShell cmdlet) with parameters and variables associated with that command. Each segment is delineated by the | character.

Powershell output is passed from left to right.

The first thing to keep in mind when looking at pipelines in PowerShell is that the output and input that is being passed between commands (cmdlets) is an object.

What is an object?

Think of it as a container that holds parameters (characteristics about the object such as name, length, etc.) and methods (functionality that the object performs such as rename, resize, copy, etc.).

Parameters are the object characteristics; Methods are the actions the object can perform
A very basic representation of a two segment pipeline.

The “dot” operator AND variables

The parameters and methods in an object are accessed during command execution by a single “dot” following the object instance name. An example of this will follow.

When an instance of an object is created, it needs to be given a unique name. This name for the object instance is called a variable. It is designated by a leading $ character.

Consider the following PowerShell example:

In the above example, the variable $objectinstance is being declared. It is an empty variable with no datatype until it is assigned a value. The cmdlet Get-Process returns an object as output that represents the “PowerShell” system process. That object instance is then connected to the variable $objectinstance. That object’s parameters and methods can now be accessed via this variable.

In the next line of the above example, we see the line $objectinstance.Name. This returns a string (a sequence of characters) output -> “powershell”. That is the name of this process object instance. The parameter called “Name” is being accessed through the dot operator and being outputted to the command prompt interface when you type $objectinstance.Name and hit enter.

The final line in the above example is the method GetType() contained within the object instance being accessed by the dot operator. As we said earlier, a method performs an operation on or for the object or its data. In this case, the method returns the type of object that $objectinstance is and some other characteristics about it. You can call the GetType() method for any object in order to find out what base type of object it is and what the type name is.

That pesky $_ variable

Have you ever seen the $_ characters used in a pipeline and wanted to shutdown your machine and take a nap? Never fear… this is nothing more than a variable name implicitly created by powershell and automatically applied to the current instance of an object that is running through the pipeline at that moment. When an object is being output by a cmdlet, the $_ variable name is simply a way for you to reference the object and to access its parameters and methods.

Viewing the Object instance’s available Methods and Parameters

You can view the object’s methods and parameters(properties) by piping the object instance to the cmdlet Get-Member. This is illustrated in the following image where $objectinstance is the same object variable used earlier in this blog post.

Piping an object to Get-Member lists the object’s members including the methods and parameters (properties).

A pipeline troubleshooting rule of thumb

If you are getting errors or unexpected output from a PowerShell pipeline, a common rule of thumb to troubleshoot the issue is to remove the right-most cmdlet/segment and to replace it with Get-Member so as to resolve what type of object is being passed down the pipe at that stage. Make sure it is the type of object you are intending to pass into the cmdlet that was replaced by Get-Member.

In future parts of this blog series, we will look at filtering and acting on objects in loops, cmdlet native filtering, parameter binding and some best practices when using PowerShell pipelines.

--

--