- Courses
- 💻Programming Languages
- 🪟PowerShell
- Foundations of PowerShell
- Commands, Cmdlets and the Pipeline
On this page
PowerShell Cmdlets and the Object-Based Pipeline
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
PowerShell is built around two interlocking concepts: cmdlets — structured, verb-noun named commands that emit .NET objects — and the pipeline — a mechanism that passes those objects between commands without any text parsing. Understanding how cmdlets work, how the pipeline streams data, and how to write pipeline-aware functions is the foundation of effective PowerShell scripting.
What Is a Cmdlet?
A cmdlet (pronounced "command-let") is a compiled .NET class that performs a single, well-defined operation. Unlike shell scripts or batch commands, a cmdlet does not read or produce plain text. It receives and emits .NET objects — structured data with typed properties and methods.
Every cmdlet follows the Verb-Noun naming convention: a standardised action verb, a hyphen, and a singular noun describing the target. Get-Process, Stop-Service, New-Item, Remove-Item, Set-Location are all examples. The verb communicates intent; the noun communicates the target.
Approved verbs
PowerShell enforces a set of approved verbs to ensure consistency across the command surface. Using an unapproved verb in a module generates a warning when the module is imported. List all approved verbs:
Get-Verb
Common verb categories:
CategoryExample verbsDataGet, Set, New, Remove, ClearLifecycleStart, Stop, Restart, Enable, DisableDiagnosticTest, Measure, Debug, TraceCommunicationSend, Receive, Connect, Disconnect
How a cmdlet runs
When PowerShell executes a cmdlet, it:
- Loads the underlying .NET class
- Binds parameter values from the command line or from the pipeline
- Calls the processing methods on the class (
BeginProcessing,ProcessRecord,EndProcessing) - Emits output objects to the pipeline or to the host
The output is never plain text until it reaches a formatting cmdlet at the end of the pipeline. At every intermediate stage, the data remains structured .NET objects.
The Object-Based Pipeline
The | character connects commands in a pipeline. When a cmdlet produces output, it emits .NET objects that the next command receives directly — without any text serialisation, parsing, or line-splitting.
Get-Process | Sort-Object CPU -Descending | Select-Object Name, CPU -First 10
At no stage in this pipeline does PowerShell convert anything to text. Get-Process emits System.Diagnostics.Process objects. Sort-Object receives those same objects, sorts them by the CPU property, and passes them on. Select-Object receives the sorted objects and projects two properties from each one.
This is what separates PowerShell from Bash and cmd.exe, where every pipeline stage receives and emits text, requiring grep, awk, and sed to parse structured data from unstructured output.
Pipeline streaming
The pipeline does not wait for the first command to finish before starting the second. It streams: as each object is emitted from the first command, it is immediately passed to the next command in the pipeline. This means:
- A long-running command like
Get-ChildItem -Recursestarts producing results thatWhere-Objectcan filter before the directory scan is complete - Memory usage stays low — the entire result set is never held in memory at once
- Output appears on screen progressively rather than after a potentially long wait
# This starts filtering and displaying before Get-ChildItem finishes
Get-ChildItem C:\Windows -Recurse -File | Where-Object Length -gt 10MB
The $_ and $PSItem Automatic Variables
Inside a pipeline expression — in Where-Object, ForEach-Object, Select-Object calculated properties, and Sort-Object custom expressions — the current pipeline object is available as $_ or its alias $PSItem. Both names refer to the same value.
# $_ represents each process object as it flows through the pipeline
Get-Process | Where-Object { $_.CPU -gt 1 }
# $PSItem is identical — use whichever is clearer to you
Get-Process | Where-Object { $PSItem.CPU -gt 1 }
$_ works inside script block arguments ({}). Outside a pipeline context, $_ is $null.
Core Pipeline Cmdlets
ForEach-Object
ForEach-Object runs a script block against each object in the pipeline. It is the pipeline-native equivalent of a foreach loop, and unlike a loop, it preserves streaming:
# Runs the script block for each process object as it arrives
Get-Process | ForEach-Object {
"$($_.Name) is using $([math]::Round($_.WorkingSet64 / 1MB, 1)) MB"
}
ForEach-Object has two important shorthand syntaxes in PowerShell 7:
# Method call shorthand — calls .Kill() on each process object
Get-Process -Name "notepad" | ForEach-Object -MemberName Kill
# Property access shorthand — equivalent to ForEach-Object { $_.Name }
Get-Process | ForEach-Object -MemberName Name
Where-Object
Where-Object filters the pipeline, passing only objects where the condition evaluates to $true:
# Script block syntax — flexible, supports any expression
Get-Process | Where-Object { $_.CPU -gt 1 -and $_.WorkingSet64 -gt 50MB }
# Comparison syntax — cleaner for simple single-property conditions
Get-Process | Where-Object CPU -gt 1
# Multiple conditions with comparison syntax (PowerShell 7+)
Get-Service | Where-Object { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic' }
Select-Object
Select-Object shapes the objects in the pipeline — selecting specific properties, adding calculated properties, and limiting the number of results:
# Select specific properties
Get-Process | Select-Object Name, Id, CPU, WorkingSet64
# Calculated property — creates a new property with a custom expression
Get-Process | Select-Object Name, @{ Name = "MemoryMB"; Expression = { [math]::Round($_.WorkingSet64 / 1MB, 1) } }
# Limit results
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
# Skip the first N objects
Get-Process | Sort-Object Name | Select-Object -Skip 10 -First 10
Sort-Object
Sort-Object reorders objects by one or more properties, working directly on typed values — numbers sort numerically, dates sort chronologically, strings sort alphabetically:
# Sort ascending (default)
Get-Process | Sort-Object CPU
# Sort descending
Get-Process | Sort-Object CPU -Descending
# Sort by multiple properties
Get-Service | Sort-Object Status, DisplayName
Group-Object
Group-Object groups pipeline objects by the value of a specified property, returning GroupInfo objects with a Name (the group value) and Group (the collection of matching objects):
# Count running vs stopped services
Get-Service | Group-Object Status
# Count processes by window style
Get-Process | Group-Object MainWindowTitle | Sort-Object Count -Descending
Output:
Count Name Group
----- ---- -----
93 Running {AdobeARMservice, AeLookupSvc, ...}
47 Stopped {AppIDSvc, AppMgmt, ...}
Tee-Object
Tee-Object splits the pipeline — passing objects through to the next stage unchanged while simultaneously saving them to a variable or file. Think of it as a T-junction in a pipe:
# Store intermediate results in $processes while the pipeline continues
Get-Process |
Tee-Object -Variable processes |
Sort-Object CPU -Descending |
Select-Object -First 5
# $processes now contains all processes; the terminal shows the top 5
$processes.Count
# Save to a file and continue processing
Get-EventLog -LogName Application -Newest 100 |
Tee-Object -FilePath "C:\Logs\events_raw.txt" |
Where-Object EntryType -eq Error
Pipeline Parameter Binding
Not every cmdlet accepts pipeline input, and those that do are selective about which parameters accept it. Two attributes on [Parameter()] control this:
ValueFromPipeline
The parameter accepts the entire pipeline object directly. PowerShell checks whether the object's type matches the parameter's type:
# Stop-Process accepts process objects directly via pipeline
Get-Process -Name "notepad" | Stop-Process
Stop-Process has an InputObject parameter with ValueFromPipeline = $true that accepts System.Diagnostics.Process objects — exactly what Get-Process emits.
ValueFromPipelineByPropertyName
The parameter accepts a value from a property on the pipeline object, where the property name matches the parameter name (or one of its aliases):
# The objects have a Name property; Stop-Service has a Name parameter
# with ValueFromPipelineByPropertyName — PowerShell matches them automatically
[PSCustomObject]@{ Name = "wuauserv" },
[PSCustomObject]@{ Name = "bits" } |
Stop-Service -WhatIf
Checking whether a parameter accepts pipeline input
# Which parameters of Stop-Service accept pipeline input?
Get-Help Stop-Service -Parameter * |
Where-Object { $_.pipelineInput -ne "false" } |
Select-Object Name, pipelineInput, type
Or inspect the parameter metadata directly:
(Get-Command Stop-Service).Parameters.Name.Attributes |
Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] } |
Select-Object ValueFromPipeline, ValueFromPipelineByPropertyName
Writing Pipeline-Aware Functions
To make a function accept pipeline input, two things are required:
- The
[CmdletBinding()]attribute on the function (making it an advanced function) - The
begin,process, andendblocks with the appropriate pipeline attributes on parameters
The begin, process, and end blocks
BlockWhen it runsTypical usebeginOnce before any pipeline inputInitialise variables, open connectionsprocessOnce for each object in the pipelineThe per-object logicendOnce after all pipeline inputFinalise, close connections, output aggregated results
Without a process block, the function only processes the last object piped to it — a common mistake.
function Get-ProcessSummary {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[System.Diagnostics.Process]$Process
)
begin {
$totalCPU = 0
$count = 0
}
process {
# Runs once per process object piped in
$totalCPU += $Process.CPU
$count++
Write-Verbose "Processing: $($Process.Name)"
}
end {
[PSCustomObject]@{
ProcessCount = $count
TotalCPU = [math]::Round($totalCPU, 2)
AverageCPU = if ($count -gt 0) { [math]::Round($totalCPU / $count, 2) } else { 0 }
}
}
}
# Use it in the pipeline
Get-Process | Get-ProcessSummary
Get-Process -Name "chrome" | Get-ProcessSummary -Verbose
ValueFromPipelineByPropertyName in a function
function Restart-NamedService {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$Name
)
process {
if ($PSCmdlet.ShouldProcess($Name, "Restart-Service")) {
Restart-Service -Name $Name
Write-Output "Restarted: $Name"
}
}
}
# Works because Get-Service emits objects with a Name property
Get-Service -Name "wuauserv", "bits" | Restart-NamedService -WhatIf
Discovering Commands and Inspecting Objects
Get-Command
Lists available cmdlets, functions, scripts, and aliases. Use it to explore the command surface or find commands that work with a specific noun:
# Find all commands
Get-Command
# Find all commands with a specific noun
Get-Command -Noun Process
# Find all commands from a specific module
Get-Command -Module Microsoft.PowerShell.Management
# Find commands that match a pattern
Get-Command -Name "*log*"
Get-Help
The primary source for understanding a cmdlet's behaviour, parameters, and examples:
# Full help with examples
Get-Help Get-Process -Full
# Just the examples
Get-Help Get-Process -Examples
# Help for a specific parameter
Get-Help Stop-Service -Parameter Name
# Update help from Microsoft (run as administrator)
Update-Help
Get-Member
Reveals the types, properties, and methods of the objects emitted by a command — essential for understanding what the pipeline is carrying:
# See all members of Process objects
Get-Process | Get-Member
# Filter to only properties
Get-Process | Get-Member -MemberType Property
# See what type the objects are
Get-Process | Get-Member | Select-Object TypeName -First 1
# TypeName: System.Diagnostics.Process
Get-Member is indispensable when building a pipeline: it tells you exactly which properties you can filter on with Where-Object, sort on with Sort-Object, or select with Select-Object.
The "Filter Left, Format Right" Principle
The single most important pipeline best practice: filter as early as possible, format only at the end.
Filtering early reduces the number of objects flowing through subsequent stages, which improves performance and reduces memory pressure. Formatting — Format-Table, Format-List, Format-Wide — converts objects into display-only text objects. Any cmdlet after a format cmdlet cannot access the original object's properties.
# WRONG — sorts and selects all 200 processes, then filters
Get-Process | Sort-Object CPU -Descending | Select-Object -First 20 | Where-Object CPU -gt 1
# CORRECT — filters first, then sorts the smaller result set
Get-Process | Where-Object CPU -gt 1 | Sort-Object CPU -Descending | Select-Object -First 20
# WRONG — Format-Table must come last; nothing useful can follow it
Get-Process | Format-Table Name, CPU | Where-Object CPU -gt 1 # Where-Object gets text, not objects
# CORRECT — format is always the final stage
Get-Process | Where-Object CPU -gt 1 | Sort-Object CPU -Descending | Format-Table Name, CPU
Practical Pipeline Examples
Top memory-consuming processes
Get-Process |
Sort-Object WorkingSet64 -Descending |
Select-Object Name, Id, @{ Name = "MemMB"; Expression = { [math]::Round($_.WorkingSet64 / 1MB, 1) } } |
Select-Object -First 10
Find stopped services that are set to start automatically
Get-Service |
Where-Object { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic' } |
Select-Object DisplayName, Status, StartType |
Sort-Object DisplayName
Count files by extension in a directory
Get-ChildItem C:\Windows\System32 -File |
Group-Object Extension |
Sort-Object Count -Descending |
Select-Object Name, Count |
Select-Object -First 10
Export process data to CSV
Get-Process |
Select-Object Name, Id, CPU, WorkingSet64 |
Export-Csv -Path "C:\Reports\processes.csv" -NoTypeInformation
Practice Commands
Run these in a PowerShell terminal and examine the output carefully. Use Get-Member after each one to understand the objects being produced:
# Discover commands
Get-Command -Noun Process
Get-Verb | Sort-Object Group
# Inspect objects
Get-Process | Get-Member
Get-Service | Get-Member -MemberType Property
# Basic pipeline
Get-Process | Sort-Object CPU -Descending | Select-Object Name, CPU -First 10
# Filtering
Get-Process | Where-Object { $_.WorkingSet64 -gt 100MB }
Get-Service | Where-Object Status -eq Running
# Grouping and counting
Get-Process | Group-Object Name | Sort-Object Count -Descending | Select-Object -First 5
# Tee-Object — branch the pipeline
Get-Process | Tee-Object -Variable allProcesses | Where-Object CPU -gt 1
# Pipeline-aware function
function Measure-PipelineObjects {
[CmdletBinding()]
param([Parameter(ValueFromPipeline)][object]$InputObject)
begin { $count = 0 }
process { $count++ }
end { Write-Output "Objects received: $count" }
}
Get-Process | Measure-PipelineObjects
Get-Service | Measure-PipelineObjects
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Understanding the PowerShell Environment
Next
Working with Objects
-
What Is a Cmdlet?
- Approved verbs
- How a cmdlet runs
-
The Object-Based Pipeline
- Pipeline streaming
-
The
$_and$PSItemAutomatic Variables -
Core Pipeline Cmdlets
ForEach-ObjectWhere-ObjectSelect-ObjectSort-ObjectGroup-ObjectTee-Object
-
Pipeline Parameter Binding
ValueFromPipelineValueFromPipelineByPropertyName- Checking whether a parameter accepts pipeline input
-
Writing Pipeline-Aware Functions
- The
begin,process, andendblocks ValueFromPipelineByPropertyNamein a function
- The
-
Discovering Commands and Inspecting Objects
Get-CommandGet-HelpGet-Member
-
The "Filter Left, Format Right" Principle
-
Practical Pipeline Examples
- Top memory-consuming processes
- Find stopped services that are set to start automatically
- Count files by extension in a directory
- Export process data to CSV
-
Practice Commands
