Count files and total bytes by extension in PowerShell
- Published: 2026.11.30
- Updated: 2026.11.30
- Windows
When a folder contains logs, exports, and images, a single file count gives you little idea what is inside. Grouping the files by extension lets you see which types occur most often and how much space their files take up.
Count files by extension
Open PowerShell in the parent folder of logs, or replace the path below. These examples work in Windows PowerShell 5.1 and PowerShell 7.
$groups = Get-ChildItem -LiteralPath '.\logs' -File -ErrorAction Stop |
Group-Object -Property {
if ($_.Extension) {
$_.Extension.ToLowerInvariant()
} else {
'(no extension)'
}
}
$groups | Sort-Object Count -Descending | Select-Object Name, Count
Each row shows an extension and the number of files in its group. For instance, run.LOG and error.log both count toward .log. A file named README goes in the no-extension group. The grouping uses the final extension, so archive.tar.gz goes under .gz.
Find which extensions use the most space
$groups | ForEach-Object {
$size = $_.Group | Measure-Object -Property Length -Sum
[pscustomobject]@{
Extension = $_.Name
Files = $_.Count
Bytes = $size.Sum
}
} | Sort-Object Bytes -Descending
The second table sorts by total file length in bytes. An extension with the most files can use less space than an extension with one large file. The total covers file contents; disk allocation can differ.
Check which files are included
The listing reads files in the specified folder. Add -Recurse to include subfolders or -Force to include hidden files you can access. An empty listing produces no groups. If PowerShell reports an access error, check the path and permissions before using the totals.
For one count and size across the entire listing, see counting files and totaling sizes with Measure-Object.
References
-
Previous article
Export PowerShell log matches to CSV with file paths and line numbers 2026.11.29
-
Next article
Find empty child folders in PowerShell, including hidden entries in the check 2026.12.01