Count files and total their size with PowerShell Measure-Object

  • Published: 2026.11.20
  • Updated: 2026.11.20
  • Windows

A folder listing can show which logs are present, but a quick inventory may need just two numbers: how many matching files there are and how many bytes they contain. Pipe the file objects from Get-ChildItem to Measure-Object to calculate both.

The examples use the Windows file system in Windows PowerShell 5.1 and PowerShell 7. Replace .\logs[demo] with your folder. The sample folder contains app.log (1000 bytes), server.log (2000 bytes), an archive subfolder containing old.log (3000 bytes), and a hidden hidden.log (4000 bytes).

Count matching files and add their sizes

Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' -ErrorAction Stop |
    Measure-Object -Property Length -Sum |
    Select-Object Count, Sum

The sample returned this result in Windows PowerShell 5.1:

Count  Sum
-----  ---
    2 3000

Count is the number of matching files. Sum adds their Length values, measured in bytes. PowerShell 7 may display the sum with decimal places, such as 3000.00; the unit is still bytes.

-File leaves directories out of the input. -Filter '*.log' selects the filename pattern, while -LiteralPath treats the brackets in the folder name literally. Keep the file objects in the pipeline: -Name would return strings and discard the file-size properties.

Include subfolders

Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' -Recurse -ErrorAction Stop |
    Measure-Object -Property Length -Sum |
    Select-Object Count, Sum

Adding -Recurse includes the sample’s archive\old.log. The count becomes 3 and the sum becomes 6000 bytes. Without it, the command measures only files immediately inside the starting folder.

Include hidden files when needed

Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.log' -Recurse -Force -ErrorAction Stop |
    Measure-Object -Property Length -Sum |
    Select-Object Count, Sum

On the sample Windows folder, -Force adds the hidden log: 4 files and 10000 bytes. It does not override access permissions. Start with the smallest relevant folder, especially when scanning subfolders.

Separate an empty match from a failed scan

For a count alone, use .Count on the measurement result:

(Get-ChildItem -LiteralPath '.\logs[demo]' -File -Filter '*.none' -ErrorAction Stop |
    Measure-Object).Count

The sample contains no *.none files, so this returns:

0

Each example uses -ErrorAction Stop on Get-ChildItem so that a path or access error stops that pipeline. If an error appears, resolve it before treating the result as a complete inventory. A missing folder is different from an existing folder with no matching files.

The size total describes the lengths of the selected files. It is not a measurement of free disk space or allocated space on disk. Use a filtered Get-ChildItem listing to inspect the files behind a total.

References