How to Count Files in a Folder Using PowerShell: 7 Practical Methods

In my years of working with PowerShell, I’ve often needed to count the files in a directory quickly. PowerShell offers several efficient ways to count files in a folder. Some methods are simple one-liners, while others offer more advanced filtering options.

In this tutorial, I will explain seven practical methods to count files in a folder using PowerShell, from basic approaches to more advanced options with filtering capabilities.

Method 1 – Using Get-ChildItem with Measure-Object

In PowerShell, you can use the Get-ChildItem with Measure-Object to count files in a folder. Most of the time, I use this method while working with this kind of requirement.

(Get-ChildItem -Path "C:\Projects" -File).Count

Note: It will not count the files from folders and subfolders recursively. For this, you need to check the second method below.

Here you can see the exact output:

PowerShell Count Files in a Folder

Alternatively, you can pipe the output to Measure-Object:

Get-ChildItem -Path "C:\Projects" -File | Measure-Object | Select-Object -ExpandProperty Count

This method works by:

  • Using Get-ChildItem to retrieve all items in the specified path
  • The -File parameter ensures we only count files (not folders)
  • Then we either directly access the Count property or pipe to Measure-Object

Check out PowerShell Test-Path

Method 2 – Recursive Count of All Files in a Folder and Subfolders

If you need to count all files within a folder, including its subfolders, then you can write the following command.

(Get-ChildItem -Path "C:\Projects" -File -Recurse).Count

This is useful when you need to know the total number of files in an entire directory structure. The -Recurse parameter tells PowerShell to look through all subfolders.

You can see the exact output in the screenshot below:

Count Files in a Folder Using PowerShell

Check out PowerShell Write to File UTF-8

Method 3 – Count Files by File Extension

Need to count only specific file types? Here’s how:

(Get-ChildItem -Path "C:\Projects" -Filter "*.pdf").Count

If you want to count files by file extension recursively, then you can use the following PowerShell cmdlets:

(Get-ChildItem -Path "C:\Projects" -Recurse -Filter "*.pdf").Count

Here is the exact output in the screenshot below:

How to Count Files in a Folder Using PowerShell

You can also use this alternative approach:

Get-ChildItem -Path "C:\Projects" | Where-Object {$_.Extension -eq ".pdf"} | Measure-Object | Select-Object -ExpandProperty Count

This is extremely helpful when you need to count specific file types like PDFs, Excel files, or images.

Check out How to Write to a File in a PowerShell Loop

Method 4 – Using the .NET Method for Performance

For very large directories, sometimes using .NET methods directly can be faster:

([System.IO.Directory]::GetFiles("C:\Projects")).Count

This method leverages the underlying .NET framework, which can provide better performance for simple counting tasks.

Method 5 – Count Files by Size Range

If you need to count files within a certain size range from a specific folder, then you can run the PowerShell command below.

Get-ChildItem -Path "C:\Projects" -File | Where-Object {$_.Length -gt 1MB -and $_.Length -lt 10MB} | Measure-Object | Select-Object -ExpandProperty Count

This example counts files between 1MB and 10MB, which is helpful when identifying files of a specific size category.

Read Write to File Line by Line in PowerShell

Method 6 – Count Files by Date

To count files created or modified during a specific time period:

# Files created in the last 7 days
Get-ChildItem -Path "C:\Projects" -File | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-7)} | Measure-Object | Select-Object -ExpandProperty Count

# Files modified in the last 30 days
Get-ChildItem -Path "C:\Projects" -File | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-30)} | Measure-Object | Select-Object -ExpandProperty Count

This is particularly useful for monitoring recent activity or for archiving purposes.

Method 7 – Create a File Count Report

Here’s a more advanced script that generates a report of file counts by extension:

$folderPath = "C:\Projects"
$fileStats = Get-ChildItem -Path $folderPath -File -Recurse | 
    Group-Object Extension |
    Select-Object Name, Count, @{Name="SizeInMB";Expression={[math]::Round((($_.Group | Measure-Object -Property Length -Sum).Sum / 1MB), 2)}}

$fileStats | Format-Table -AutoSize

This script:

  1. Gets all files recursively
  2. Groups them by extension
  3. Calculates the count and total size for each extension
  4. Displays the results in a neat table

Check out Write to File without Carriage Return in PowerShell

Display File Count Progress for Large Directories in PowerShell

When working with very large directories, it’s helpful to see progress:

$folderPath = "C:\Projects"
$fileCount = 0
$activity = "Counting files in $folderPath"

Get-ChildItem -Path $folderPath -File -Recurse | ForEach-Object {
    $fileCount++
    if ($fileCount % 100 -eq 0) {
        Write-Progress -Activity $activity -Status "Files counted: $fileCount"
    }
    $_
} | Measure-Object | Select-Object -ExpandProperty Count

Write-Output "Total files: $fileCount"

This script shows a progress bar that updates every 100 files, which is particularly useful when scanning massive directories.

PowerShell provides several ways to count files in a folder, including recursive counting, filtering by various criteria, and creating detailed reports.

In this tutorial, I have explained how to count files in a folder using PowerShell.

I hope you found this article helpful. Let me know in the comments if you have any questions or if there are other PowerShell file operations you’d like to learn about!

You may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App