Powershell : Pipelines Explained, Part -2


HOW PIPELINES WORK :

Piping” the object means, sending the objects as the output of one command to another command, Windows PowerShell tries to associate the piped objects with one of the parameters of the receiving cmdlet.

To do so, the Windows PowerShell “parameter bindingcomponent, which associates input objects
with cmdlet parameters, tries to find a parameter that meets the following three criteria’s :

1. The receiving cmdlet’s parameter must accept input from a pipeline (not all do)
2. The piped object type must match with receiving parameter type.
3. The parameter must not already be used in the command.

EXAMPLE :

The Start-Service cmdlet has many parameters, but only two of them, Name and InputObject
accept pipeline input. The Name parameter takes strings and the InputObject parameter takes
service objects.

Following commands show you which parameters of a cmdlet accepts pipeline inputs :

pipe

If the parameter binding component of Windows PowerShell cannot associate the piped objects with a parameter of the receiving cmdlet, the command fails and Windows PowerShell prompts you for the missing parameter values.

Please note, you cannot force the parameter binding component to associate the piped objects with a particular
parameter.

USAGE : 

PowerShell cmdlets are designed in a way to utilize the power of pipelines.
For example, you can usually pipe the results of a Get cmdlet to an action cmdlet (such as a Set,
Start, Stop, or Rename cmdlet) for the same noun.

This command pipeline starts the WMI service on the computer:

get-service wmi | start-service

Utility cmdlets, such as Get-Member, Where-Object, Sort-Object, Group-Object,
and Measure-Object are used almost exclusively in pipelines. You can pipe any objects to
these cmdlets.

For example, you can pipe all of the processes on the computer to the Sort-Object command
and have them sorted by the number of handles in the process.

get-process | sort-object -property handles

Also, Objects can be piped to the formatting cmdlets, such as Format-List and Format-Table,
and the Export cmdlets, such as Export-Clixml and Export-CSV,
and the Out cmdlets, such as Out-Printer.

For example, you can pipe the Winlogon process to the Format-List cmdlet to display all
of the properties of the process in a list.

get-process winlogon | format-list -property *

Bit of practice is required to master pipelines, and you’ll find that combining simple commands into pipelines saves time and typing, and makes your scripting more efficient.

Happy Learning ! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s