Count Items in a Folder With PowerShell

Rohan Timalsina Apr 15, 2022
Count Items in a Folder With PowerShell

PowerShell supports different file and folder operations such as creating, copying, moving, removing, and listing files and folders. Sometimes, you might need to know how many files and folders are present in the specific folder.

File Explorer is a simple method for counting files and folders in the directory. But, do you know how to count the number of files and folders using PowerShell?

This tutorial will teach you to count items in a folder with PowerShell.

Use the Measure-Object Cmdlet to Count Items in a Folder With PowerShell

The Measure-Object cmdlet calculates the property values of certain types of objects. You can use this cmdlet to count objects.

It also calculates the Average, Sum, Minimum, Maximum, and Standard Deviation of numeric values. You can count the number of string objects’ lines, words, and characters.

The following command counts the number of files and folders present in C:\New. It counts all files and folders on the first level of the folder tree.

(Get-ChildItem 'C:\New' | Measure-Object).Count

Output:

47

It means there are 47 files and folders in the directory C:\New.

The Get-ChildItem gets all items in the specified location. Then, it is piped to Measure-Object, which counts the items in a folder and prints the result.

The Count property only displays the count values.

If you want only to count files, you can use the -File parameter. It tells Get-ChildItem to get a list of files only.

For example, this command counts only files present in the directory C:\New.

(Get-ChildItem -File 'C:\New' | Measure-Object).Count

Output:

41

Similarly, you can count only folders using the -Directory parameter.

(Get-ChildItem -Directory 'C:\New' | Measure-Object).Count

Output:

6

The -Recurse parameter in Get-ChildItem gets the items recursively in the specified location. You can use the -Recurse parameter to count files and folders recursively.

(Get-ChildItem -Recurse 'C:\New' | Measure-Object).Count

Output:

12082

It counts all files and folders in the specified location, including sub-folders. We hope this article helps you understand how to count items in a folder with PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Folder