Home » PowerShell Tips » PowerShell – Count Files in Folder using Get-ChildItem

PowerShell – Count Files in Folder using Get-ChildItem

The Get-ChildItem in the PowerShell cmdlet get items and child items in one or more specified location specified by the Path parameter.

To get count files in the folder using PowerShell, Use the Get-ChildItem command to get total files in directory and use the Measure-Object cmdlet to get count files in a folder.

(Get-ChildItem -File | Measure-Object).Count  
PowerShell Count files in folder
PowerShell Count files in folder

In this article, I will walk you through using the PowerShell Get-ChildItem cmdlet to get count of files in folder or count the number of files in the current directory recursively.

PowerShell Count Files in Folder

Use the PowerShell Get-ChildItem cmdlet to get items in folder and subfolders and pass output to the Measure-Object cmdlet to perform a calculation on objects’ property values.

(Get-ChildItem -File | Measure-Object).Count

The above PowerShell command counts files in a folder and prints them on the PowerShell console.

PowerShell Count Files in Folder and SubFolders

To get number of files in folder and subfolders, use the Get-ChildItem cmdlet Recurse parameter to recursively get files in the directory and subdirectories. Run below command

(Get-ChildItem -File -Recurse| Measure-Object).Count 

In the above PowerShell command, the Get-ChildItem cmdlet gets the items in the folder and subfolders recursively and pipes the output to another command.

The second command Measure-Object cmdlet performs a count over object property values and gets the count of the number of files in folders and subfolders.

PowerShell Count Files in Folders and Subfolders by Extension

A folder may contain different extension files. To get count file in folder and subfolders by extension in PowerShell, use the Get-ChildItem cmdlet to recursively search for the File type.

It gets File objects and pipes the output to the second command.

Second command group Extension -NoElement group by file objects by extension and pass output to the third command

Third command sort count descending sort the file count in descending order.

Get-ChildItem -Recurse -File | group Extension -NoElement  | sort Count -Descending

The output of the above PowerShell command is below.

Count Name
----- ----
  297 .ts
   68 .svg
   19 .template
   10 .ps1
    9 .json
    8 .yml
    7 .png
    5 .md
    4 .js
    3 .gif

Count Files in Multiple Folders

To count number of files in multiple folders, run the following command

(Get-ChildItem -File -Recurse .\Zip\,.\FTP-101\ | Measure-Object).Count

In the above PowerShell command, Get-ChildItem gets files in multiple folders specified and pipes output to the second command.

Measure-Object cmdlets get the count number of files in multiple folders.

Cool Tip: How to find the large-size files in PowerShell!

Conclusion

I hope the above article to get count files in folder or subfolder helps you to understand the PowerShell Get-ChildItem cmdlet.

You can read more about how to use PowerShell mkdir and PowerShell New-Item to create a directory in PowerShell.

PowerShell Tip: How to find the startup folder path on Windows Server!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.