PowerShell – Count Files in Folders

Count files in folder in PowerShell

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:\Test contains three folders (FolderA, FolderB and FolderC) and four text files (File1.txt, File2.txt, File3.txt, and File4.txt).
  • FolderA contains four text files (File1.txt, File2.txt, File3.txt, and File4.txt)
  • FolderB contains two text files (File1.txt, and File2.txt)
  • FolderC has one text file (File1.txt)

Let’s practice the Measure-Object in different ways below.

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 -Recurse parameter with Get-ChildItem.

To count only Files in Folder, use -File parameter.

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.

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).

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.

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.

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.

and

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.

Count only folders inside folder in PowerShell

Use the -Directory parameter as given below to count directories only.

We can use the following command for a more organized, short, clean output.

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.

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:

That’s all about how to count files in folders in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *