The Object Pipeline
How objects flow through the PowerShell pipeline: filter, select, sort, group and measure them, keep formatting last, export results, and understand parameter binding and streaming.
By The Dark DebuggerUpdated 22 min read
The pipeline is where PowerShell earns its keep. This article covers the cmdlets you'll chain
together every day (Where-Object, Select-Object, Sort-Object, Group-Object,
Measure-Object, ForEach-Object), the rule about formatting that trips up almost everyone,
how to export results, and the two mechanisms underneath: parameter binding and streaming. You
should be comfortable with cmdlets, parameters and Get-Member
first. Variables appear lightly here and properly in
Variables and Data Types.
Why the pipeline matters
Picture a build server whose disk keeps filling up. The releases folder holds every build
artifact of a service called shop-api, each with a .sha256 checksum file, and someone has to
answer: how much space do they take, which ones are old, and what can be deleted safely? In a
text shell you'd parse ls or du output. In PowerShell you ask questions of file objects, one
pipeline stage at a time, and the answer at the end is still structured data you can save as CSV
or JSON.
The demo folder used throughout has two directories:
releases: nineshop-api-<version>.tar.gzfiles from June to November 2026 (38 to 51 MB each) plus a.sha256file for each.logs:access.log,access.log.1,access.log.2.gz,error.log,payment service.logandworker.log, with realistic web and application log lines.
Objects flowing through the pipeline
The | operator passes the output objects of one command to the next command, one object at a
time. Get-ChildItem emits System.IO.FileInfo objects, and Get-Member shows what each one
carries:
Get-ChildItem releases | Get-Member -MemberType Property -Name Name, Length, LastWriteTime, Extension
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Name Property string Name {get;}
Length Property long Length {get;}
LastWriteTime Property datetime LastWriteTime {get;set;}
Extension Property string Extension {get;}
Every cmdlet in this article works on those properties by name. Inside a script block, the
current object is $_. $PSItem is a longer alias for the same thing, which some people find
more readable:
Get-ChildItem logs | Where-Object { $PSItem.Extension -eq '.log' } | ForEach-Object Nameaccess.log
error.log
payment service.log
worker.logFiltering with Where-Object
Where-Object keeps the objects for which a condition is true. The full form takes a script
block:
Get-ChildItem releases | Where-Object { $_.Length -gt 45MB }
Directory: /home/sid/demo/releases
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
-rw-r--r-- sid sid 10/06/2026 14:30 49353086 shop-api-1.7.0.tar.gz
-rw-r--r-- sid sid 10/27/2026 14:30 49417052 shop-api-1.7.1.tar.gz
-rw-r--r-- sid sid 11/03/2026 14:30 53509163 shop-api-1.8.0-rc1.tar.gz
For a single comparison there's a simplified syntax without braces or $_:
Get-ChildItem releases | Where-Object Length -gt 45MB | Select-Object Name, Length
Name Length
---- ------
shop-api-1.7.0.tar.gz 49353086
shop-api-1.7.1.tar.gz 49417052
shop-api-1.8.0-rc1.tar.gz 53509163
Combining conditions needs the script block form. Here, tarballs released before August:
Get-ChildItem releases | Where-Object { $_.Name -like '*.tar.gz' -and $_.LastWriteTime -lt (Get-Date '2026-08-01') } | Select-Object Name, LastWriteTime
Name LastWriteTime
---- -------------
shop-api-1.4.0.tar.gz 06/02/2026 14:30:00
shop-api-1.4.1.tar.gz 06/23/2026 14:30:00
shop-api-1.5.0.tar.gz 07/14/2026 14:30:00
The comparison operators (-gt, -like, -and and many more) get their own article:
Comparison, Logical, and Arithmetic Operators.
Filter left
If a command can filter for you, let it. -Filter on Get-ChildItem is applied by the file
system while it enumerates, while Where-Object sees every object after PowerShell has built
it. I timed both finding the 5,211 .gz files under /usr/share on my lab machine:
(Measure-Command { Get-ChildItem /usr/share -Recurse -Filter *.gz -ErrorAction Ignore }).TotalMilliseconds
(Measure-Command { Get-ChildItem /usr/share -Recurse -ErrorAction Ignore | Where-Object Name -like '*.gz' }).TotalMilliseconds941.8652
2493.2653Same result, 2.6 times slower. Against a remote system (Active Directory, a REST API, a database) the gap is far bigger, because filtering late means transferring everything first. The rule is filter left, format right: filter as early as possible, format at the very end.
Selecting properties with Select-Object
Select-Object picks which properties to keep, and how many objects:
Get-ChildItem releases -Filter *.tar.gz | Select-Object -Last 2 Name, LastWriteTime
Name LastWriteTime
---- -------------
shop-api-1.7.1.tar.gz 10/27/2026 14:30:00
shop-api-1.8.0-rc1.tar.gz 11/03/2026 14:30:00
-First, -Last and -Skip limit the count; -Unique drops duplicates.
Calculated properties
When the property you want doesn't exist, compute it with a hashtable holding a Name and an
Expression script block:
Get-ChildItem releases -Filter *.tar.gz |
Select-Object Name,
@{Name = 'SizeMB'; Expression = { [math]::Round($_.Length / 1MB, 1) }},
@{Name = 'Released'; Expression = { $_.LastWriteTime.ToString('yyyy-MM-dd') }}
Name SizeMB Released
---- ------ --------
shop-api-1.4.0.tar.gz 38.10 2026-06-02
shop-api-1.4.1.tar.gz 38.10 2026-06-23
shop-api-1.5.0.tar.gz 41.10 2026-07-14
shop-api-1.5.1.tar.gz 41.20 2026-08-04
shop-api-1.6.0.tar.gz 44.20 2026-08-25
shop-api-1.6.1.tar.gz 44.10 2026-09-15
shop-api-1.7.0.tar.gz 47.10 2026-10-06
shop-api-1.7.1.tar.gz 47.10 2026-10-27
shop-api-1.8.0-rc1.tar.gz 51.00 2026-11-03
A pipeline can end a line after | or , and continue on the next, which keeps long pipelines
readable. (PowerShell 7.6's table view displayed these rounded doubles with two decimals; the
underlying values are 38.1, 41.2 and so on, as the CSV export later shows.) The short keys
n and e also work: @{n='SizeMB'; e={...}}.
-ExpandProperty: values, not objects
Select-Object Name doesn't give you names. It gives you new objects that have a Name
property. Usually that difference is invisible, until you put the result in a string:
$latest = Get-ChildItem releases -Filter *.tar.gz | Select-Object -Last 1 Name
"Deploying $latest"Deploying @{Name=shop-api-1.8.0-rc1.tar.gz}-ExpandProperty returns the property's value itself:
$latest = Get-ChildItem releases -Filter *.tar.gz | Select-Object -Last 1 -ExpandProperty Name
"Deploying $latest"Deploying shop-api-1.8.0-rc1.tar.gzUse -ExpandProperty whenever the next step wants plain values: a string, a file path, a list
of names to pass to another command.
Sorting with Sort-Object
Get-ChildItem releases -Filter *.tar.gz | Sort-Object Length -Descending | Select-Object -First 3 Name, Length
Name Length
---- ------
shop-api-1.8.0-rc1.tar.gz 53509163
shop-api-1.7.1.tar.gz 49417052
shop-api-1.7.0.tar.gz 49353086
PowerShell 7 can do the "sort then take the first N" in one step with -Top (or -Bottom),
with the same output:
Get-ChildItem releases -Filter *.tar.gz | Sort-Object Length -Descending -Top 3 | Select-Object Name, Length-Top doesn't exist in Windows PowerShell 5.1, which says
Sort-Object : A parameter cannot be found that matches parameter name 'Top'. Use the
Select-Object -First form in scripts that must run on both.
To sort by several keys in different directions, pass hashtables:
Get-ChildItem logs | Sort-Object Extension, @{Expression = 'Length'; Descending = $true} | Select-Object Name, Extension, Length
Name Extension Length
---- --------- ------
access.log.1 .1 2129964
access.log.2.gz .gz 211648
access.log .log 2420077
payment service.log .log 926458
worker.log .log 514014
error.log .log 36023
Sort by type, not by text
Sorting compares values by their type. Strings sort character by character, which is wrong for version numbers:
'1.10.0', '1.9.2', '1.4.1' | Sort-Object1.10.0
1.4.1
1.9.2A script block as the sort key lets you convert first. Cast to [version]:
'1.10.0', '1.9.2', '1.4.1' | Sort-Object { [version]$_ }1.4.1
1.9.2
1.10.0Keep that in mind for the CSV mistake later in this article: numbers read back from a CSV file are strings too.
Grouping with Group-Object
Group-Object buckets objects by a property and counts them:
Get-ChildItem logs | Group-Object Extension
Count Name Group
----- ---- -----
1 .1 {/home/sid/demo/logs/access.log.1}
1 .gz {/home/sid/demo/logs/access.log.2.gz}
4 .log {/home/sid/demo/logs/access.log, /home/sid/…
The Group property holds the original objects of each bucket. If you only want counts, add
-NoElement. The grouping key can also be a script block, which makes "per month" reports a
one-liner:
Get-ChildItem releases -Filter *.tar.gz | Group-Object { $_.LastWriteTime.ToString('yyyy-MM') } -NoElement
Count Name
----- ----
2 2026-06
1 2026-07
2 2026-08
1 2026-09
2 2026-10
1 2026-11
Grouping works just as well on text you've split apart. HTTP status codes are the ninth
space-separated field of each line in access.log:
Get-Content logs/access.log | ForEach-Object { $_.Split(' ')[8] } | Group-Object -NoElement | Sort-Object Count -Descending
Count Name
----- ----
16199 200
2391 304
1163 404
423 401
423 502
401 500
That's 824 server errors (500 and 502) out of 21,000 requests, worth a closer look. With
-AsHashTable, Group-Object returns a lookup table keyed by group name instead:
$byExt = Get-ChildItem logs | Group-Object Extension -AsHashTable followed by
$byExt['.gz'].Name printed access.log.2.gz. Hashtables are covered in
Arrays, Hashtables, and Custom Objects.
Measuring with Measure-Object
Measure-Object computes count, sum, average, minimum and maximum of a numeric property:
Get-ChildItem releases -Filter *.tar.gz | Measure-Object -Property Length -Sum -Average -Maximum -Minimum
Count : 9
Average : 45676212.1111111
Sum : 411085909
Maximum : 53509163
Minimum : 39931778
StandardDeviation :
Property : Length
(-StandardDeviation is another switch; Windows PowerShell 5.1 doesn't have it.) For text,
it counts lines, words and characters, like wc:
Get-Content logs/error.log | Measure-Object -Line -Word -Character
Lines Words Characters Property
----- ----- ---------- --------
700 3729 35323
Doing work per object with ForEach-Object
ForEach-Object runs a script block once per object. With -Begin and -End you get setup and
summary blocks that run once:
Get-ChildItem releases -Filter *.tar.gz | ForEach-Object -Begin { $total = 0 } -Process { $total += $_.Length } -End { "{0:N1} MB in releases" -f ($total / 1MB) }392.0 MB in releasesBuilding a line of text per object is a common use. The -f format operator pads and formats
numbers (details in String Formatting, Split, Join, Replace):
Get-ChildItem logs -Filter *.log | ForEach-Object { "{0,-22} {1,8:N0} KB" -f $PSItem.Name, ($PSItem.Length / 1KB) }access.log 2,363 KB
error.log 35 KB
payment service.log 905 KB
worker.log 502 KBWhen all you need is one property or one method call, pass its name instead of a script block:
Get-ChildItem logs | ForEach-Object Name prints the six names. PowerShell 7 also has
ForEach-Object -Parallel for running the block concurrently, covered in
Remoting, Background Jobs, and Parallelism.
Formatting is the last step
Format-Table, Format-List and Format-Wide exist to make output pleasant for people. They
work by replacing your objects with formatting instructions. Anything placed after them receives
those instructions, not your data. Here's what lands in a CSV if you format first:
Get-ChildItem releases -Filter *.tar.gz | Format-Table Name, Length | Export-Csv bad.csv
Get-Content bad.csv | Select-Object -First 3"ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
"033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
"9e210fe47d09416682b841769c78b8a3",,,,,Those are the internal format objects, written out faithfully. Sorting after formatting is worse, because it doesn't even fail. This "sorted by length" table isn't sorted:
Get-ChildItem logs | Format-Table Name, Length | Sort-Object Length
Name Length
---- ------
access.log 2420077
access.log.1 2129964
access.log.2.gz 211648
error.log 36023
payment service.log 926458
worker.log 514014
The rule: Format-* goes last, or not at all. Use Select-Object to choose columns for data,
and only format when the next consumer is a human (the screen, or a text report).
Sending results somewhere
Data cmdlets take objects and keep them as data:
Get-ChildItem releases -Filter *.tar.gz | Select-Object Name, Length, LastWriteTime | Export-Csv releases.csv
Get-Content releases.csv | Select-Object -First 3"Name","Length","LastWriteTime"
"shop-api-1.4.0.tar.gz","39931778","06/02/2026 14:30:00"
"shop-api-1.4.1.tar.gz","39999286","06/23/2026 14:30:00"Import-Csv turns the file back into objects, but every value comes back as a string, since CSV
has no types:
(Import-Csv releases.csv)[0].Length.GetType().NameStringJSON keeps numbers as numbers and writes dates in ISO 8601 format:
Get-ChildItem releases -Filter *.tar.gz | Select-Object -First 2 Name, Length, LastWriteTime | ConvertTo-Json[
{
"Name": "shop-api-1.4.0.tar.gz",
"Length": 39931778,
"LastWriteTime": "2026-06-02T14:30:00-04:00"
},
{
"Name": "shop-api-1.4.1.tar.gz",
"Length": 39999286,
"LastWriteTime": "2026-06-23T14:30:00-04:00"
}
]Two Windows PowerShell 5.1 differences show up here. 5.1 serialises dates in an old Microsoft
format: the same kind of Select-Object ... | ConvertTo-Json on a Windows file gave
"LastWriteTime": "\/Date(1622894933406)\/". And 5.1's Export-Csv and ConvertTo-Csv add a
first line like #TYPE Selected.System.IO.FileInfo unless you pass -NoTypeInformation, which
is the default behaviour in PowerShell 7.
When the output is for people, a text file of a formatted table is fine, because formatting is
still the last step before the file:
Get-ChildItem logs | Sort-Object Length -Descending | Format-Table Name, Length -AutoSize | Out-File log-sizes.txt
wrote exactly the table you'd see on screen. The whole subject of CSV, JSON and XML has its own article:
CSV, JSON, and XML.
How parameter binding works
When an object arrives through the pipeline, PowerShell has to decide which parameter of the next command receives it. It tries in this order:
- ByValue: the whole object goes to a parameter that accepts pipeline input "by value" and has a matching type.
- ByPropertyName: a property of the object goes to a parameter with the same name (or alias) that accepts input "by property name".
- The same two steps again, this time allowing type conversion.
ByValue is why strings can be piped into Get-Item. Its -Path parameter takes
string[] by value:
'logs/error.log', 'logs/worker.log' | Get-Item | Select-Object Name, Length
Name Length
---- ------
error.log 36023
worker.log 514014
ByPropertyName is the more powerful one. Say a colleague hands you a cleanup list as CSV,
with a Path column:
Import-Csv cleanup.csv
Path Reason
---- ------
logs/access.log.2.gz rotated more than 48 hours ago
releases/shop-api-1.4.0.tar.gz superseded by 1.4.1
releases/shop-api-1.4.0.tar.gz.sha256 superseded by 1.4.1
Remove-Item's -Path accepts input by property name (we checked its metadata in
the previous article), so each row's Path
property binds to it with no glue code at all:
Import-Csv cleanup.csv | Remove-Item -WhatIfWhat if: Performing the operation "Remove File" on target "/home/sid/demo/logs/access.log.2.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.4.0.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.4.0.tar.gz.sha256".The Reason column is simply ignored, because Remove-Item has no -Reason parameter.
When binding doesn't do what you expect, Trace-Command shows the decisions. The full trace is
long; these are the key lines for piping a string into Get-Item:
Trace-Command -Name ParameterBinding -PSHost -Expression { 'logs/error.log' | Get-Item }DEBUG: 2026-11-09 21:54:01.8796 ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Get-Item]
DEBUG: 2026-11-09 21:54:01.8796 ParameterBinding Information: 0 : PIPELINE object TYPE = [System.String]
DEBUG: 2026-11-09 21:54:01.8796 ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: 2026-11-09 21:54:01.8796 ParameterBinding Information: 0 : Parameter [Path] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: 2026-11-09 21:54:01.8797 ParameterBinding Information: 0 : BIND arg [logs/error.log] to parameter [Path]
[…]
DEBUG: 2026-11-09 21:54:01.8798 ParameterBinding Information: 0 : BIND arg [System.String[]] to param [Path] SUCCESSFUL"ValueFromPipeline NO COERCION" is step 1: a string, bound by value to a string parameter without conversion.
Streaming versus collecting
Most cmdlets stream: each object travels down the whole pipeline before the next one is
produced. Write-Host makes the order visible:
1..3 | ForEach-Object { Write-Host "produce $_"; $_ } | ForEach-Object { Write-Host " consume $_" }produce 1
consume 1
produce 2
consume 2
produce 3
consume 3Some cmdlets have to see everything before emitting anything. You can't sort what you haven't
seen, so Sort-Object collects:
1..3 | ForEach-Object { Write-Host "produce $_"; $_ } | Sort-Object | ForEach-Object { Write-Host " consume $_" }produce 1
produce 2
produce 3
consume 1
consume 2
consume 3Group-Object and Measure-Object collect too. Streaming is why Select-Object -First can stop
a pipeline early. This generator never ends on its own, yet the command returns at once:
& { while ($true) { Get-Random -Maximum 1000 } } | Select-Object -First 3572
392
438It's also why how you read a file matters. Get-Content streams lines; wrapping it in
parentheses reads the whole file into memory first:
(Measure-Command { Get-Content logs/access.log | Select-Object -First 5 }).TotalMilliseconds
(Measure-Command { (Get-Content logs/access.log) | Select-Object -First 5 }).TotalMilliseconds0.5819
224.1584Half a millisecond against 224 ms for a 2.4 MB file. On a multi-gigabyte log, the collecting version can exhaust memory. Collect when you need everything (sorting, counting, reusing a list); stream otherwise.
Where you'll use this
Release retention on a build server
This script answers the question from the start of the article: which artifacts fall outside the newest three, how much space they use, and a record of the decision in CSV and JSON for whoever approves the cleanup:
# release-report.ps1 - which release artifacts fall outside the retention window?
param(
[string]$Path = './releases',
[int]$Keep = 3
)
$artifacts = Get-ChildItem -Path $Path -Filter *.tar.gz | Sort-Object LastWriteTime -Descending
$old = $artifacts | Select-Object -Skip $Keep
$report = $old | Select-Object Name,
@{Name = 'SizeMB'; Expression = { [math]::Round($_.Length / 1MB, 1) }},
@{Name = 'Released'; Expression = { $_.LastWriteTime.ToString('yyyy-MM-dd') }},
@{Name = 'AgeDays'; Expression = { ((Get-Date) - $_.LastWriteTime).Days }}
$report | Export-Csv -Path ./release-cleanup.csv
$report | ConvertTo-Json | Set-Content -Path ./release-cleanup.json
$freed = ($old | Measure-Object -Property Length -Sum).Sum
"{0} of {1} artifacts are outside the newest {2}; removing them frees {3:N1} MB" -f
$old.Count, $artifacts.Count, $Keep, ($freed / 1MB)
$report | Format-Table -AutoSize./release-report.ps16 of 9 artifacts are outside the newest 3; removing them frees 246.8 MB
Name SizeMB Released AgeDays
---- ------ -------- -------
shop-api-1.6.1.tar.gz 44.10 2026-09-15 55
shop-api-1.6.0.tar.gz 44.20 2026-08-25 76
shop-api-1.5.1.tar.gz 41.20 2026-08-04 97
shop-api-1.5.0.tar.gz 41.10 2026-07-14 118
shop-api-1.4.1.tar.gz 38.10 2026-06-23 139
shop-api-1.4.0.tar.gz 38.10 2026-06-02 160
Get-Content ./release-cleanup.csv | Select-Object -First 3"Name","SizeMB","Released","AgeDays"
"shop-api-1.6.1.tar.gz","44.1","2026-09-15","55"
"shop-api-1.6.0.tar.gz","44.2","2026-08-25","76"Notice where formatting happens: once, on the last line, for the human reading the console. The
CSV and JSON got clean data. The deletion itself is the same pipeline ending in Remove-Item;
the file objects bind to it directly, and -WhatIf shows exactly what would go:
Get-ChildItem releases -Filter *.tar.gz | Sort-Object LastWriteTime -Descending | Select-Object -Skip 3 | Remove-Item -WhatIfWhat if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.6.1.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.6.0.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.5.1.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.5.0.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.4.1.tar.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.4.0.tar.gz".Process reports
The same cmdlets work on any object. Which processes on this machine use the most memory?
Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 5 Name, Id,
@{Name = 'MemMB'; Expression = { [math]::Round($_.WorkingSet64 / 1MB) }},
@{Name = 'CPUsec'; Expression = { [math]::Round($_.CPU, 1) }}
Name Id MemMB CPUsec
---- -- ----- ------
java 1656 827.00 380.50
java 39031 438.00 57.70
java 35641 236.00 25.40
java 35685 230.00 28.40
pwsh 40812 183.00 8.40
On my lab machine the Java processes of the Jenkins test instances top the list. On a Windows server
the same pipeline, with Get-Service, Get-WinEvent or Get-CimInstance at the front, produces
service, event-log and hardware reports. The pattern never changes: get objects, filter, shape,
sort or group, then output.
Common mistakes
Combining conditions in the simplified Where-Object syntax
Get-ChildItem releases | Where-Object Length -gt 45MB -and Name -like '*rc*'Where-Object: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.The simplified syntax isn't an expression; it's a set of parameters
(-Property Length -GT -Value 45MB) that describes exactly one comparison. The extra words leave
PowerShell unable to work out which comparison you meant, hence the parameter-set error. Use a
script block:
Where-Object { $_.Length -gt 45MB -and $_.Name -like '*rc*' }.
A CSV column that doesn't match the parameter name
Here the cleanup list's column is called File, not Path:
Import-Csv cleanup-files.csv | Remove-Item -WhatIfRemove-Item: Cannot find path '/home/sid/demo/@{File=logs/access.log.2.gz; Reason=rotated more than 48 hours ago}' because it does not exist.
Remove-Item: Cannot find path '/home/sid/demo/@{File=releases/shop-api-1.4.0.tar.gz; Reason=superseded by 1.4.1}' because it does not exist.No property matched by name, so binding fell through to step 3: convert the whole object to a
string and bind it by value. That string, @{File=...; Reason=...}, isn't a path. Rename the
property on the way through with a calculated property:
Import-Csv cleanup-files.csv | Select-Object @{Name = 'Path'; Expression = { $_.File }} | Remove-Item -WhatIfWhat if: Performing the operation "Remove File" on target "/home/sid/demo/logs/access.log.2.gz".
What if: Performing the operation "Remove File" on target "/home/sid/demo/releases/shop-api-1.4.0.tar.gz".Sorting numbers that came back from a CSV
Get-ChildItem logs | Select-Object Name, Length | Export-Csv log-sizes.csv
Import-Csv log-sizes.csv | Sort-Object Length -Descending | Select-Object -First 3
Name Length
---- ------
payment service.log 926458
worker.log 514014
error.log 36023
The three largest files are 2.4 MB, 2.1 MB and 0.9 MB, but this says the largest is 926 KB.
Length came back as a string, and as text "9..." beats "5..." beats "3..." beats
"2...". Convert in the sort key:
Import-Csv log-sizes.csv | Sort-Object { [long]$_.Length } -Descending | Select-Object -First 3
Name Length
---- ------
access.log 2420077
access.log.1 2129964
payment service.log 926458
Summary
- The pipeline passes objects one at a time;
$_(or$PSItem) is the current object inside script blocks. - Filter left, format right: use a command's own filter parameters before
Where-Object, and keepFormat-*for the very end. Where-Object Prop -op Valuehandles one condition; anything with-and/-orneeds a script block.Select-Objectshapes objects (properties,-First/-Last/-Skip, calculated properties);-ExpandPropertyreturns bare values.Sort-Object,Group-ObjectandMeasure-Objectcollect all input before emitting; most other cmdlets stream, which is whySelect-Object -Firstcan stop a pipeline early.- Sorting follows type: cast strings to
[version]or[long]in a script-block key, especially afterImport-Csv. - Pipeline input binds ByValue first, then ByPropertyName; matching column or property names to parameter names lets CSV rows drive cmdlets directly.
Export-CsvandConvertTo-Jsonkeep data as data; 5.1 adds a#TYPEline to CSV and writes JSON dates as\/Date(...)\/.
Exercises
List the five largest files in logs, largest first, showing Name and a SizeKB column
rounded to whole kilobytes.
Show Solution
Get-ChildItem logs -File | Sort-Object Length -Descending | Select-Object -First 5 Name, @{Name = 'SizeKB'; Expression = { [math]::Round($_.Length / 1KB) }}
Name SizeKB
---- ------
access.log 2363.00
access.log.1 2080.00
payment service.log 905.00
worker.log 502.00
access.log.2.gz 207.00
Sort on the real Length before you create the rounded column, so ties and rounding can't
change the order. If you want the column to show whole numbers in the table, use
[int]($_.Length / 1KB) instead of [math]::Round.
In logs/access.log, the request path is the seventh space-separated field (index 6). Show the
three most requested paths with their counts.
Show Solution
Get-Content logs/access.log | ForEach-Object { $_.Split(' ')[6] } | Group-Object -NoElement | Sort-Object Count -Descending | Select-Object -First 3
Count Name
----- ----
2153 /healthz
2146 /api/orders/1842
2136 /api/orders
Health checks top the list, which is typical: load balancers and Kubernetes probes hit
/healthz constantly. Splitting on spaces is fine for this log format; for messier text,
Regular Expressions is the better tool.
Lines in logs/worker.log look like
2026-11-09T00:00:01 WARN [worker] slow query took 2140 ms. Count how many lines there are per
level (INFO, WARN, ERROR), most common first.
Hint
There are two spaces after WARN (the level is padded), so a plain Split(' ') gives you an
empty field. [StringSplitOptions]::RemoveEmptyEntries drops empty fields.
Show Solution
Get-Content logs/worker.log | ForEach-Object { $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)[1] } | Group-Object -NoElement | Sort-Object Count -Descending
Count Name
----- ----
8425 INFO
1103 WARN
272 ERROR
Field 0 is the timestamp, field 1 the level. Because Get-Content streams, this works the same
on a log file many times larger.
For the .tar.gz files in releases, produce one row per month (yyyy-MM) with the number of
releases and their total size in MB, rounded to one decimal.
Hint
Group by a script block, then use a calculated property that runs Measure-Object on each
group's Group property.
Show Solution
Get-ChildItem releases -Filter *.tar.gz |
Group-Object { $_.LastWriteTime.ToString('yyyy-MM') } |
Select-Object Name, Count, @{Name = 'TotalMB'; Expression = { [math]::Round(($_.Group | Measure-Object Length -Sum).Sum / 1MB, 1) }}
Name Count TotalMB
---- ----- -------
2026-06 2 76.20
2026-07 1 41.10
2026-08 2 85.40
2026-09 1 44.10
2026-10 2 94.20
2026-11 1 51.00
Inside the calculated property, $_ is a group object, and $_.Group holds the original
FileInfo objects of that month. Nesting pipelines like this is normal and readable once you
know where each $_ points.
Tested with: PowerShell 7.6.9 on Ubuntu 24.04 (WSL2), and Windows PowerShell
5.1.26100.9711 on Windows 11, November 2026. Every PowerShell 7 command ran as shown in a
100-column terminal against the demo releases and logs folders; the release artifacts are
sparse files with realistic sizes, and the Trace-Command output is trimmed to the relevant
lines. The 5.1 notes (-Top missing, the \/Date(...)\/ JSON format, the #TYPE CSV line, no
-StandardDeviation) come from read-only commands in Windows PowerShell 5.1 on the host. Timings
depend on the machine; the ratios are what matter. The surprise while testing: sorting after
Format-Table produced no error at all in 7.6, just an unsorted table. Paths from the test machine's scratch directories are shown shortened to /home/sid/….
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every PowerShell tutorial here is tested end to end before publishing.