August 26, 2015

PowerShell - Pipeline

One of the fundamental concepts in PowerShell is pipeline. It is a series of commands where the output of one becomes the input of the next, like a production assembly in a factory. It allows you to make refinements on output while being passed between the stages. For this you have to put a pipe '|' sign in-between the commands.

Lets start using it:
Get-Process | Sort-Object Name
Here the output of Get-Process command is become input for Sort-Object command which will sort the items by Name.

Lets add one more command Select-Object to the chain and select only desired columns.
Get-Process | Sort-Object Name | Select-Object Id,Name,Desciption

Add command Group-Object for grouping by name
Get-Process | Sort-Object Name | Select-Object Id,Name,Desciption | Group-Object Name | Select-Object Count, Name
 
Now add ConvertTo-Html to get output in HTML format.
Get-Process | Sort-Object Name | Select-Object Id,Name,Desciption | Group-Object Name | Select-Object Count, Name | ConvertTo-Html

Add Out-File to put all this output in HTML file.
Get-Process | Sort-Object Name | Select-Object Id,Name,Desciption | Group-Object Name | Select-Object Count, Name | ConvertTo-Html | Out-File "C:\Test\test.html"

Open file in browser to see the content.
Invoke-Expression "C:\Test\test.html"

You can find a no of commands which can be used with pipeline to produce the results more meaningful.

No comments:

Post a Comment