Skip to content

Pipelines

image.png

  • Definition of Pipeline

    • A pipeline is a series of commands linked by pipeline operators (|).
    • Output of one command becomes input for the next.
    • Commands are processed left to right, and the final result is displayed in the console.
  • Cmdlets and Pipelines

    • Most PowerShell cmdlets support the pipeline.
    • You can pass results from a Get cmdlet to another cmdlet with the same noun.
    • The receiving cmdlet must have a parameter that accepts pipeline input.
  • How Cmdlets Accept Pipeline Input

    • By value → parameter accepts a value matching the expected .NET type.
    • By property name → parameter accepts input if the object has a property with the same name.
    • Use Get-Help <cmdlet> to see which parameters accept pipeline input.
  • Automatic Enumeration

    • Objects implementing IEnumerable are automatically enumerated.
    • Members are sent one at a time through the pipeline.
    • Hashtable requires calling the GetEnumerator() method.
  • Conditional Pipelines (PowerShell 7+)

    image.png

    • && (double ampersand) → executes the right command only if the left command succeeds.

    • || (double vertical bar) → executes the right command only if the left command fails.

    • Uses $LastExitCode to determine success/failure, applicable to cmdlets, functions, and native console commands.

      image.png

      image.png

      image.png

PS C:\Users\PavanKumarBandaru> Get-Process -Name "Notepad" | Stop-Process -Verbose VERBOSE: Performing the operation "Stop-Process" on target "Notepad (24932)".

this will kill all notepad process return by Get-Porcess, not just first or 1 process

basically Stop-Process applied repeatedly on all results of |

image.png

  • Streams in PowerShell

    • PowerShell has multiple streams: success, error, warning, verbose, debug, etc.

    • The pipeline only passes objects through the success stream.

    • Other messages (errors, warnings) are separated so they don’t mix with pipeline data.

      image.png

  • Common Cmdlets for Streams

    • Write-Output → sends data to the success stream (can be piped).
    • Write-Host → sends messages directly to the host, not the streams.
      • Behavior depends on the host program.
      • In PowerShell 5+, Write-Host wraps Write-Information.
  • Better User Communication

    • Write-Verbose → sends messages to the verbose stream.
    • Users can see verbose messages by using the Verbose parameter or changing $VerbosePreference.
    • Recommended over writing to the success stream or using Write-Host.
  • Formatting Output

    • Format cmdlets help control how object properties are displayed.
    • Format-Wide → shows one property, arranged in columns.
    • Format-List → shows each property on a separate line, supports wildcards.
    • Format-Table → shows multiple properties in table form, good for a detailed view.
    • Format cmdlets can be combined with Select-Object in a pipeline.
      • Select-Object lets you pick properties or create custom properties using logic with curly braces.

        image.png

  • Key Takeaways

    • Keep pipeline data and user messages separate.
    • Use Write-Verbose for user messages instead of Write-Host.
    • Format cmdlets + Select-Object = flexible, clean output.

image.png

OUTPUT

image.png

image.png

Format-View Examples..

image.png

image.png

image.png