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.
Related Article - PowerShell Folder
- Get the Size of the Folder Including the Subfolders in PowerShell
- PowerShell Compare Folders
- Set Folder Permissions in PowerShell
- Delete Empty Folders in PowerShell
- Recursively Set Permissions on Folders Using PowerShell
- Open a Folder Using PowerShell