Table of Contents
Count Files in Folders in PowerShell
We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, before moving forward, it is essential to know how many files and folders we have on the different paths that we will use in this article.
E:\Testcontains three folders (FolderA,FolderBandFolderC) and four text files (File1.txt,File2.txt,File3.txt, andFile4.txt).FolderAcontains four text files (File1.txt,File2.txt,File3.txt, andFile4.txt)FolderBcontains two text files (File1.txt, andFile2.txt)FolderChas one text file (File1.txt)
Let’s practice the Measure-Object in different ways below.
|
1 2 3 |
Write-Host ( Get-ChildItem -Path E:\Test | Measure-Object ).Count; |
|
1 2 3 |
7 |
The above command counts the files and folders from the specified path, so it counted 3 folders and 4 files in E:\Test folder.
Please note that it will count files only in specified folders and not subfolders. To count files in all the subfolders as well, use
-Recurseparameter withGet-ChildItem.
To count only Files in Folder, use -File parameter.
|
1 2 3 |
Write-Host ( Get-ChildItem -Path E:\Test -File | Measure-Object ).Count; |
|
1 2 3 |
4 |
We used the -Path parameter to write the path of the desired location. Remember that we can omit the -Path parameter if there is only one location’s path; otherwise, using the -Path parameter is required (we’ll see another example with multiple paths later).
We can even omit the location’s path if we use this command to count files and folders from the current directory. To practice, let’s move to the E:\Test directory; you can go ahead to your desired location and use the following command.
|
1 2 3 |
Write-Host ( Get-ChildItem | Measure-Object ).Count; |
|
1 2 3 |
7 |
See, it still returned 7. Now, how did this command work? Let’s understand that starting with the Measure-Object cmdlet calculates the object’s property values. We used it to count objects; in our case, objects can be files, folders, or both. We can also use it to count objects with a given Property.
We can also use the Measure-Object to calculate Average, Sum, Minimum/Maximum, and Standard Deviation for numeric values. However, we can use Measure-Object to count the number of words, lines and characters if we are working with String objects.
We used .Count to get the count of the files/folders. Next, we used Write-Host to write customized output to the host, which means displaying the required output on the PowerShell console.
Remember that the Write-Host cmdlet does not produce the pipeline output, so we can not forward the Write-Host‘s output to another cmdlet using pipeline (also called pipe represented as |).
If you have the requirement to send your output to the next cmdlet using pipeline, then use Write-Output cmdlet, which can write the given objects to the pipeline. Next, we have the Get-ChildItem cmdlet, which we can use to get the items and child items from one or multiple locations.
Count Files in multiple Folders in PowerShell
In the above code, we used one location to get items/child items; see the following example if you want to count items/child items from multiple locations. This is where specifying the -Path parameter is mandatory because now we have multiple paths (array of paths).
|
1 2 3 |
Write-Host ( Get-ChildItem -Path "E:\Test", "E:\Test\FolderA" | Measure-Object ).Count; |
|
1 2 3 |
11 |
This time, we got 11 because the above command counted the files and folders from E:\Test (which has three folders and four text files) and E:\Test\FolderA (which has four text files). Therefore, we can use the above command using the array operator (@()) as follows.
|
1 2 3 |
Write-Host ( Get-ChildItem -Path @("E:\Test", "E:\Test\FolderA") | Measure-Object ).Count; |
|
1 2 3 |
11 |
Count Files in Folders and Subfolders in PowerShell
To count files in Folders and SubFolders in PowerShell, use -Recurse parameter with Get-ChildItem cmdlet and pipe it with Measure-Object cmdlet.
For this article, we have only three folders in E:\Test but think, if we have hundreds of folders, would it be an excellent approach to use the array operator or array to specify a path for each folder? Of course not!
In that case, we can use the -Recurse parameter to get all items in all child containers.
|
1 2 3 |
Write-Host ( Get-ChildItem "E:\Test" -Recurse | Measure-Object ).Count; |
|
1 2 3 |
14 |
For the above output, it counted all files and folders from E:\Test, E:\Test\FolderA, E:\Test\FolderB, and E:\Test\FolderC, but what if more subdirectories (folders inside the folders). In that case, we may have to restrict the number of levels to recurse in PowerShell. To do that, we can use the -Depth parameter.
To practice the -Depth parameter, let’s create another folder named FolderD inside the FolderA and create four text files in FolderD. Now, execute the following pair of commands and compare both results.
|
1 2 3 4 |
Write-Host ( Get-ChildItem "E:\Test" -Recurse -Depth 1 | Measure-Object ).Count; Get-ChildItem "E:\Test" -Recurse -Depth 1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
15 Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:30 PM FolderA d----- 12/29/2022 9:13 PM FolderB d----- 12/29/2022 9:13 PM FolderC -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:21 PM FolderD -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderB Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt Directory: E:\Test\FolderC Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt |
and
|
1 2 3 4 |
Write-Host ( Get-ChildItem "E:\Test" -Recurse -Depth 2 | Measure-Object ).Count; Get-ChildItem "E:\Test" -Recurse -Depth 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
19 Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:30 PM FolderA d----- 12/29/2022 9:13 PM FolderB d----- 12/29/2022 9:13 PM FolderC -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:21 PM FolderD -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA\FolderD Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderB Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt Directory: E:\Test\FolderC Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt |
See, we can only count the files from E:\Test\FolderA\FolderD if we have -Depth 2; this is what the -Depth parameter is used for. Remember, the Get-ChildItem cmdlet will not display the empty subdirectories; if it is used with the -Recurse or -Depth parameter, it will not include the empty directories in the output.
Now, what if we only want to count files? We can do that using the -File parameter as follows.
|
1 2 3 4 |
Write-Host ( Get-ChildItem "E:\Test" -Recurse -Depth 2 -File| Measure-Object ).Count; Get-ChildItem "E:\Test" -Recurse -Depth 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
15 Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:30 PM FolderA d----- 12/29/2022 9:13 PM FolderB d----- 12/29/2022 9:13 PM FolderC -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:21 PM FolderD -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA\FolderD Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderB Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt Directory: E:\Test\FolderC Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt |
Further reading:
Count only folders inside folder in PowerShell
Use the -Directory parameter as given below to count directories only.
|
1 2 3 4 |
Write-Host ( Get-ChildItem "E:\Test" -Recurse -Depth 2 -Directory| Measure-Object ).Count; Get-ChildItem "E:\Test" -Recurse -Depth 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
4 Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:30 PM FolderA d----- 12/29/2022 9:13 PM FolderB d----- 12/29/2022 9:13 PM FolderC -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/29/2022 9:21 PM FolderD -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderA\FolderD Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt -a---- 12/28/2022 11:33 PM 0 File3.txt -a---- 12/28/2022 11:33 PM 0 File4.txt Directory: E:\Test\FolderB Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt -a---- 12/28/2022 11:33 PM 0 File2.txt Directory: E:\Test\FolderC Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 12/28/2022 11:33 PM 0 File1.txt |
We can use the following command for a more organized, short, clean output.
|
1 2 3 4 |
ls -rec | ? {$_.mode -match 'd'} | select FullName, @{N='Count';E={(ls $_.FullName | measure).Count}} |
|
1 2 3 4 5 6 7 8 |
FullName Count -------- ----- E:\Test\FolderA 5 E:\Test\FolderB 2 E:\Test\FolderC 1 E:\Test\FolderA\FolderD 4 |
In the above code fence, measure is an alias of Measure-Object. We can also use the ls command (it is used to list the directory content) as follows to count files and folders from the parent directory only, which is E:\Test in our case.
|
1 2 3 4 5 |
write-host (ls "E:\Test").Count <#we can also write as follows#> (ls).count |
|
1 2 3 4 |
7 7 |
Count Files by Extension in Folder in PowerShell
To count File by Extension in Folder in PowerShell, use group Extension -NoElement with Get-ChildItem cmdlet.
Here is an example:
|
1 2 3 |
Get-ChildItem -Recurse -File | group Extension -NoElement |
|
1 2 3 4 5 |
Count Name ----- ---- 11 .txt |
That’s all about how to count files in folders in PowerShell.