Pipelines
Pipeline
The output of one command can be used as input for another command. The PowerShell pipeline
chains together a number of commands similar to a production assembly. So, one command hands over its result to the next, and at the end, you receive the result.
The pipeline is the concept in PowerShell to link together commands with pipeline operator (|). That means result from the first command is sent down the pipeline as input to the second command for further processing and the result of the second command is sent down to the pipeline as input to the third command and so on and so forth. For example:
Command-1 | Command-2 | Command-3
In this example, the objects that Command-1 emits are sent to Command-2. Command-2 processes the objects and sends them to Command-3. Command-3 processes the objects and send them down the pipeline. Because there are no more commands in the pipeline, the results are displayed at the console.
Object-orientated
A pipeline in PowerShell is a true object-oriented pipeline
so the results from a command remain rich objects. Only at the end of the pipeline will the results be reduced to text or HTML or whatever you
choose for output.
Some typical pipeline cmdlets and functions
Cmdlet or Function | Description |
---|---|
Export-Csv | Saves objects in a comma-separated values file |
ForEach-Object | Returns each pipeline object one after the other |
Format-List | Outputs results as a list |
Format-Table | Outputs results as a table |
Select-Object | Filters properties of an object and limits number of results as requested |
Sort-Object | Sorts results |
Where-Object | Filters results according to a criterian |
Example: Using pipelines for an array
In the following example we will use Sort
and Get-unique
methods for an array. Firstly the methods are used seperately, and secondly they are combined together as a pipeline.
#create an array
$names = 'Al', 'Ben', 'Cecilia' ,'Dave', 'Al'
#sort
$names | Sort
#get unique items without sort does not work
$names | Get-Unique
#use Sort and Get-Unique with a pipeline works
$names | Sort | Get-Unique
Example: Using pipelines for Dir-command
Step 1: Only Dir-command
Dir
Step 2: Sorting by LastWriteTime
Dir | Sort-Object LastWriteTime
Step 3: Showing in table selected columns Sorted by LastWriteTime
Dir | Sort-Object LastWriteTime | Format-Table Name, LastWriteTime
Grouping Information
Group-Object works by grouping similar objects and then reporting their number.
Specify the property to Group-Object as grouping criterion.
Example
: The next line returns nicely status
overview of services.
Get-Service | Group-Object Status
Filtering
It is possible to filter results of commands. For example Get-Service returns all services, and the list can be very looooooooooooong. So let's filter result with like-operator, that we see only services starting with 'App'
Get-Service | Where-Object Name -like App*
Example: Filtering and Grouping
In the follwing example we will first filter services and secondly group those filtered services.
Get-Service | Where Name -like *user* | Group-Object Status
Example: Filtering, Sorting and Formatting
In the follwing example png files will be first filtered by size and secondly sorted, and thirdly formatted as table. This pipeline consists of four commands in the specified order.
Get-ChildItem -Path *.png |
Where-Object {$_.length -gt 8000} |
Sort-Object -Property length |
Format-Table -Property name, length
Example: Get Gateway
In the follwing example we will use a pipeline to get the gateway of LAN.
ipconfig.exe | Select-String -Pattern 'Gateway'
One-liner
The one-liner
is one continuous pipeline. It is not necessarily a command that’s on one physical line. Not all commands that are on one physical line are one-liners.
The following command is on more than one physical line, it’s a PowerShell one-liner because it’s one continuous pipeline.
Please, note that numbers in the beginning are not the part of a one-liner command. They are presenting only that the oneliner is on three lines.
1 PS C:\> Get-Service |
2 Where-Object CanPauseAndContinue -eq $TRUE |
3 Select-Object -Property DisplayName, ServiceName