Windows Powershell runs commands in a pipeline. Each command line we see is a pipeline. A pipeline can contain one or more commands, with multiple commands separated by a vertical pipe character ” | ” .
Commands execute from left to right, with the output of each command being piped or passed to the command after it. The output of the last command in the pipeline is what appears on your screen.
Get-Service #it is a single commad pipeline
#Multicommand pipelines looks similar
Get-Service| out-file servicelist.txt
Get-Service |out-file servicelist.txt doesn’t show any output do you know why?
Well, it stores the output directly to the text file mentioned after the pipeline. If you do understand this then you are on the right path.
You must have understood the concept of Pipelining. Now let’s understand how we can use pipeling with select-object, sort-object, and measure-object. Let’s see how we can use the above pipelines with some examples.
Sorting-Objects on a Property
Sort-Object can re-sort objects in the pipeline.
Get-Service | Sort-Object Name Descending
Similarly, you can try Ascending in the above and try the output.
Measuring-Object on a Property
Measure-Object can measure objects in the pipeline. We need to add -Property to specify the single numeric property. After -Property, we can add the following.
The Output will be a measurable or measurement object but not whatever we piped in. Let’s check out few examples you can try on your PC.
Get-Service | Measure-Object
Get-process | Measure-Object
2,0,5,6 | Measure-Object -Sum
2,0,5,6 | Measure-Object -maximum
Special Uninstaller is a great application to delete or uninstall software that is not required. The plus point is it clears every file and registry key associated with the application. It is available for Free and Premium with 24×7 support. Do try this application out.
Selecting a Subset of an Object
Select-object has no intelligence whatsoever and as the name suggests it is used for selection let’s check what it is actually used in and how we can use it in a script.
Below are some parameters which can be used with Select-Object.
We cannot specify any criteria for choosing any specific rows. Let’s check out some examples we can try on our PC.
Get-Service | select-object -first 10
Get-process | select-object -first 10
This is one way of using Select-Object or Select, let’s check out another way we can use Select-object.
get-service |select-object -property Name -first 10
In this above example, we are selecting a specific property i.e. we have chosen Name and also selected the first 10 results. Similarly, we can try the same with other examples.
In this tutorial, we have understood what is Pipeline in Powershell, why to use it and how we can use it. We have also gone through selecting, sorting, and measuring objects and where we can use them with examples. You can try each example on your PC but make sure you have Powershell installed in it. Let’s meet you in our next blog soon and I hope you have understood something from this today.