Use Cmdlet Pipeline in Windows PowerShell

Q

How to use cmdlet pipeline in Windows PowerShell?

✍: FYIcenter.com

A

You can use the pipeline operator "|" to combine multiple cmdlets into a sequence of executions. The pipeline operator will send the output from the previous cmdlet to the next cmdlet as the input.

Here is the syntax of cmdlet pipeline:

cmdlet-1 | cmdlet-2 | ... | cmdlet-n

    Output of cmdlet-1 becomes input of cmdlet-2
    ...
    Output of cmdlet-n-1 becomes input of cmdlet-n

Example 1: The following cmdlet pipeline finds the Notepad process and stops it:

get-process notepad | stop-process

Here is how the about cmdlet pipeline works:

  • The get-process cmdlet returns the Notepad process object as the output.
  • The pipeline operator passes the Notepad process object to the stop-process cmdlet.
  • The stop-process stops the process represented by Notepad process object.

Example 2: The following cmdlet pipeline finds all *.txt files that are larger than 10,000 bytes, then display them as a two-column table sorted by file size:

Get-ChildItem -path *.txt | Where-Object {$_.length -gt 10000} 
   | Sort-Object -property Length | Format-Table -property name,length

Here is how the about cmdlet pipeline works:

  • The "Get-ChildItem -path *.txt" cmdlet returns a list of objects represents all *.txt files.
  • The "Where-Object {$_.length -gt 10000}" cmdlet filters the list of objects to keep *.txt files that larger than 10,000 bytes.
  • The "Sort-Object -property Length" cmdlet sorts the list of objects by file size.
  • The "Format-Table -property name,length" cmdlet displays the list of objects as a two-column table.

The final output may look like this:

        Name                       Length
        ----                       ------
        tmp1.txt                    82920
        tmp2.txt                   114000
        tmp3.txt                   114000

 

Application Command in Pipeline in Windows PowerShell

Cmdlet Output Object in Windows PowerShell

Introduction of Windows PowerShell Cmdlet

⇑⇑ Windows PowerShell Tutorials

2016-11-05, 1756🔥, 0💬