Skip to main content
infervour.com

Back to all posts

How to Count Files In Folder And Subfolder Using Powershell?

Published on
3 min read
How to Count Files In Folder And Subfolder Using Powershell? image

Best PowerShell Scripts to Buy in October 2025

1 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$31.99 $44.99
Save 29%
Learn Windows PowerShell in a Month of Lunches
2 PowerShell in Depth

PowerShell in Depth

BUY & SAVE
$59.99
PowerShell in Depth
3 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$40.91 $44.99
Save 9%
Learn Windows PowerShell in a Month of Lunches
4 PowerShell with SharePoint from Scratch: Exercises and Explanations (Microsoft Tech from Scratch)

PowerShell with SharePoint from Scratch: Exercises and Explanations (Microsoft Tech from Scratch)

BUY & SAVE
$9.99
PowerShell with SharePoint from Scratch: Exercises and Explanations (Microsoft Tech from Scratch)
5 PowerShell in Depth: An Administrator's Guide

PowerShell in Depth: An Administrator's Guide

BUY & SAVE
$105.82
PowerShell in Depth: An Administrator's Guide
6 Instant Windows PowerShell 3.0 Windows Management Instrumentation Starter

Instant Windows PowerShell 3.0 Windows Management Instrumentation Starter

BUY & SAVE
$21.99
Instant Windows PowerShell 3.0 Windows Management Instrumentation Starter
7 Microsoft Exchange Server PowerShell Cookbook - Third Edition

Microsoft Exchange Server PowerShell Cookbook - Third Edition

BUY & SAVE
$51.99
Microsoft Exchange Server PowerShell Cookbook - Third Edition
+
ONE MORE?

To count files in a folder and its subfolders using Powershell, you can use the following command:

(Get-ChildItem -Recurse -File -Path "C:\Path\To\Folder" | Measure-Object).Count

Replace "C:\Path\To\Folder" with the actual path to the folder you want to count the files in. This command will recursively count all the files within the specified folder and its subfolders.

How to export the results of counting files in folders and subfolders to a CSV file with PowerShell?

To export the results of counting files in folders and subfolders to a CSV file with PowerShell, you can use the following script:

$directory = "C:\Path\To\Root\Folder" $results = Get-ChildItem $directory -Recurse | Where-Object { $_.PSIsContainer } | ForEach-Object { [PSCustomObject]@{ FolderName = $_.FullName NumberOfFiles = (Get-ChildItem $_.FullName -File).Count } }

$results | Export-Csv "C:\Path\To\OutputFile.csv" -NoTypeInformation

Replace "C:\Path\To\Root\Folder" with the path to the root folder where you want to start counting the files. This script will recursively search through all folders and subfolders within the specified root folder, count the number of files in each folder, and export the results to a CSV file named "OutputFile.csv" in the specified directory.

Run this script in the PowerShell console to generate the CSV file with the results of counting files in folders and subfolders.

How to count files in a folder and subfolder using PowerShell without using recursion?

To count files in a folder and subfolders without using recursion, you can use the following PowerShell command:

(Get-ChildItem -Path "C:\path\to\folder" -Recurse -File | Measure-Object).Count

This command uses the Get-ChildItem cmdlet to retrieve all files in the specified folder and its subfolders without recursion. The -Recurse parameter tells PowerShell to search the subfolders as well. Measure-Object is then used to count the number of files returned by Get-ChildItem.

What is the PowerShell command to exclude specific file extensions while counting files in a folder and subfolder?

To exclude specific file extensions while counting files in a folder and subfolder using PowerShell, you can use the following command:

(Get-ChildItem -Path "C:\Path\To\Folder" -Recurse -File | Where-Object {!($_.Extension -match ".jpg|.png")}).Count

In this command:

  • Get-ChildItem -Path "C:\Path\To\Folder" -Recurse -File is used to get all the files in the specified folder and subfolders.
  • Where-Object {!($_.Extension -match ".jpg|.png")} filters out the files with extensions .jpg and .png.
  • .Count counts the number of files remaining after filtering out the specified file extensions.

You can modify the extensions in the Where-Object clause to exclude different file extensions as needed.