Powershell – Pipeline

 
Pipeline concept

Pipeline concept, enables the output of one cmdlet to be piped as input to another cmdlet.

Other Shell languages use the pipe concept, but PowerShell is different in that you pipe objects and not their text-representations.

In Powershell pipeline structured .NET objects are passed between stages in the pipeline instead of typically unstructured text. Using objects eliminates the need to explicitly parse text output to extract data.

 Powershell - Pipeline_s001

Powershell uses the pipeline to send a stream of objects, thereby encapsulating all the information and methods within the object in an easily an accessible way.

 

$_

The current pipeline object; used in script blocks, filters, the process clause of functions, where-object, foreach-object and switch

Commonly used on a pipeline with corresponding cmdlet member property, method or event. Examples: $_.length, $_.name or $_.tostring

Note: To see cmdlet members (in this case for Get-Process cmdlet) type:


Get-Process | Get-Member

 

Why using?

 

Generate easier and more readable solutions

You can replace this code


$a = Get-ChildItem *.txt

foreach ($file in $a)

{

if ($file.length -gt 100)

{

Write-Host $file.name

}

}

With this one


Get-ChildItem *.txt | where {$_.length -gt 100} | Format-Table name

 

Create complex and useful scripting solutions…

A simple example:


Get-Process ‘

| Where-Object {$_.company -Notlike '*Microsoft*'} ‘

| Format-Table ProcessName, Company

A even simpler example:


Get-Process | Out-Host -Paging

 


In Depth
http://technet.microsoft.com/en-us/library/dd347728.aspx
http://technet.microsoft.com/en-us/library/ee176927.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/10/use-the-pipeline-to-create-robust-powershell-functions.aspx
https://www.simple-talk.com/sql/sql-tools/the-posh-dba-grown-up-powershell-functions/

Tested With
PowerShell 2.0

Advertisements
Categories: Powershell | Leave a comment

Post navigation

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

Blog at WordPress.com.

%d bloggers like this: