fbpx

How to Use PowerShell pipeline And Its Functions? 2 Best Ways

One of the most significant and practical components of the PowerShell shell and scripting language is the PowerShell pipeline. Once you have a rudimentary understanding of how it operates, you can use its strength for your own purposes.

How to Use PowerShell pipeline And Its Functions? 2 Best Ways

With the help of the PowerShell pipeline, you can combine commands to create a single “pipeline” that streamlines code and supports parallel processing. Continue reading to find out more about the pipeline and how to create functions that take advantage of it.

How Windows PowerShell pipeline Work?

The majority of PowerShell commands take input from a parameter. The command accepts an item as input and uses it to perform an action. It then has the option to produce some object.

Pipeline commands behave like human runners in a relay race. The baton (items) is passed from one runner to the next during the race, with the exception of the first and last.

Pipeline Command Parameter Binding

The pipeline is considerably more difficult. Commands only accept input through arguments. Even if you don’t explicitly define a parameter, the pipeline must somehow know which one to use.


Parameter binding is the process of determining the parameter to utilize when a command accept pipeline input. The incoming command’s parameter(s) must support it for an object coming in from the pipeline to be correctly bound to a parameter. 

There are two ways to facilitate pipeline parameter binding with command parameters: by value and/or by property name.

1. Pipeline Parameter ByValue

The full receiving object is accepted as the parameter value for the command parameter. A ByValue parameter searches the incoming objects for an item of a particular type. 
If the object type matches, PowerShell accepts the object on the grounds that it is intended to be bound to that parameter.


The Path parameter of the Get-ChildItem cmdlet accepts a string object type and pipelines input via ByValue. 

  • For instance, because C:Windows is a string, running something like ‘C:Windows’ | Get-ChildItem returns all files in the C:Windows directory.

2. Pipeline Parameter ByPropertyName

The command parameter only accepts a single attribute of the object, not the full object. This is accomplished by looking at the property name rather than the object type.

The Name option of the Get-Process cmdlet is configured to receive PowerShell pipeline input by property name. PowerShell matches or binds the Name property on the incoming object to the Name argument and uses that value when you send an object with a Name property to the Get-Process cmdlet, such as “[pscustomobject]@Name=’firefox’ | Get-Process.”

How to Build Your Own Commands In The PowerShell Pipeline Function?

1. Allowing Pipeline Input

You must first define the relevant parameter attribute in order for this function to take PowerShell pipeline input. In order to receive pipeline input by value, this attribute can either be set to ValueFromPipeline or ValueFromPipelineByPropertyName.

As in the example below, put the ValueFromPipeline parameter attribute between the [Parameter()] definition’s brackets.


 [Parameter(ValueFromPipeline)]
 [string[]]$ComputerName
image 1 9

Any string object given to the Get-ConnectionStatus function will now be bound to the ComputerName argument. However, just because parameter binding is happening doesn’t indicate the function will use it in any useful ways.

2. Adding a Process Block 

Next, add a Process block to instruct PowerShell to handle all objects arriving from the pipeline. This block instructs PowerShell to handle every item that enters the pipeline.

PowerShell will only process the first object it receives from the pipeline in the absence of a Process block. PowerShell is instructed to keep processing objects by the Process block.

Enclose the entire function’s functionality to add a Process block.

function Get-ConnectionStatus
{
    [CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline)]
        [string[]]$ComputerName
    )

    Process ## New Process block
    {
        foreach($c in $ComputerName)
        {
            if(Test-Connection -ComputerName $c -Quiet -Count 1)
            {
                $status = 'Ok'
            }
            else
            {
                $status = 'No Connection'
            }

            [pscustomobject]@{
                ComputerName = $c
                Status = $status
            }
        }
    } ## end Process block
}
Adding a Process Block

Send output exclusively from the Process block. A PowerShell command can receive objects from the pipeline by sending output from the Process block, which “streams” the objects out to the pipeline.

Passing Objects to the PowerShell Pipeline

As soon as the function above has a Process block constructed, you may call it by using the PowerShell pipeline and giving arguments for the ComputerName parameter.

Get-ConnectionStatus -ComputerName '127.0.0.1', '192.168.1.100'
## or

'127.0.0.1', '192.168.1.100' | Get-ConnectionStatus
Get-ConnectionStatus -ComputerName '127.0.0.1', '192.168.1.100'
## or

'127.0.0.1', '192.168.1.100' | Get-ConnectionStatus

At this point, you may start using the PowerShell pipeline true potential and start adding more commands to the mix. You have a text file called C:Testcomputers.txt that has a line of IP addresses that are separated from one another as seen below.

127.0.0.1
192.168.1.100

Then, you could retrieve each of those IP addresses from the text file using the Get-Content cmdlet and give them straight to the Get-ConnectionStatus function.

Get-ConnectionStatus | Get-Content -Path C\:Test.txt 
Get-ConnectionStatus | Get-Content -Path C\:Test.txt 

By piping the items that Get-ConnectionStatus returns directly to the ForEach-Object cmdlet, you might take this arrangement a step further.


Get-Content -Path C:\Test\computers.txt |
Get-ConnectionStatus |
ForEach-Object { Write-Host "$($_.ComputerName) connection status is: $($_.Status)" }
Get-ConnectionStatus | Get-Content -Path C\:Test.txt 

PowerShell would not report any status to the console until all objects (IP addresses) were processed if PowerShell pipeline input wasn’t enabled on the ComputerName argument or if Get-ConnectionStatus didn’t return an object within the Process block.

Pipeline Binding by Property Name

Get-ConnectionStatus is configured to take pipeline input by value (ValueFromPipeline), which accepts an array of strings like “127.0.0.1” and “192.168.1.100.” 

When the CSV is imported and sent through the pipeline to Get-ConnectionStatus, an unexpected response is returned in the ComputerName field.

Import-Csv -Path 'C:\Test\pc-list.csv' | Get-ConnectionStatus
Import-Csv -Path 'C:\Test\pc-list.csv' | Get-ConnectionStatus

The parameter declaration for the function’s ComputerName parameter reads [Parameter(ValueFromPipeline)]. As a result, in order to set the ComputerName parameter to enable input by property name, as illustrated below, you must also include ValueFromPipelineByPropertyName.

[CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string[]]$ComputerName
  )
[CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string[]]$ComputerName
  )


You instruct PowerShell to start looking at the object property names and the object type once you have PowerShell pipeline support for ByPropertyName. After making this adjustment, you ought to see the desired result.

PS> Import-Csv -Path 'C:\Test\pc-list.csv' | Get-ConnectionStatus
PS> Import-Csv -Path 'C:\Test\pc-list.csv' | Get-ConnectionStatus