Counting the Number of Files or Subfolders Using PowerShell

Written by Eric Wyatt (last updated December 28, 2020)

1

As more and more items are stored on our computers, it is easy to lose count of how many items are contained in one folder. We have discussed in other articles limitations when it comes to folder sizes. While the likelihood of reaching 4,294,967,295 files in a folder is slim to none, there are times that we still need to know the number of files or subfolders within a folder.

If your folder only contains a small number of items, this can be easily done by opening the folder and subsequent subfolders and manually counting everything. However, if your folder has subfolders and those folders contain numerous subfolders themselves, counting manually can be tedious. This is where PowerShell comes in handy. By using a simple command, you can have your system automatically do the counting for you.

To do this you need to launch PowerShell. These steps assume that you have PowerShell set to be shown in the secondary Start menu. If not, look at this article explaining how to configure PowerShell to be shown.

  1. Right-click on the Start button. Windows displays the secondary Windows Start menu.
  2. Choose Windows PowerShell. Windows opens a PowerShell window.
  3. Enter the file structure to the folder you're wanting to check by entering "cd" (without quotes) followed by the folder location. Hint: after entering "cd" (again, without quotes) add a space and then drag the folder you want to look at the PowerShell window. Windows places the correct folder path for you. Then press Enter. You will see "PS C:\FILEPATH"Ê
  4. Enter the following command and press Enter. This counts all the files or subfolders within the parent folder you specified:
  5. (Get-ChildItem -Recurse | Measure-Object).Count
    
  6. Windows returns the number of all the files and subfolders.Ê
  7. When done, close the PowerShell window.

Related Commands

There are some slightly similar commands that might be useful as you're looking to count folder contents. To use these commands, just enter them instead of the command in step 4 above.

Get the count of the items only in the parent directory, does not return the counts of the subfolder elements:

Get-ChildItem | Measure-Object | %{$_.Count}

Count all the subfolders within a parent directory:

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

Count all the files within a parent directory (even those within subfolders, without adding the subfolders to the count):

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

Knowing these commands will help you determine the number of items within a parent folder. All much faster, and less tedious than counting manually.

 This tip (13817) applies to Windows 10.

Author Bio

Eric Wyatt

Eric Wyatt is a swell guy (or so his friends tell him). He is a formally trained designer and branding expert, bringing a wide range of skills to his Tips.Net articles. ...

MORE FROM ERIC

Getting Started with the Mail App

The Mail app is a great way to access your e-mail. This tip shows you how to get started quickly and easily.

Discover More

Five Windows 10 Services that are Safe to Disable

Windows 10 is built using services that perform tasks. In this tip you'll discover five services that you can disable safely.

Discover More

Controlling the Taskbar on Multiple Monitors

On Windows computers the Taskbar, by default, runs along the bottom of the screen. Learn how to control how the Taskbar ...

Discover More
More WindowsTips

Passing Parameters to a PowerShell Script

Like the older batch-file processor, PowerShell can accept parameters. This allows for flexibility in your script. This ...

Discover More

Change your Computer's Name with PowerShell

Your computer has a name that is separate from your account name. You can use PowerShell to change the name of your computer.

Discover More

Using PowerShell to Find Your Wireless Signal Strength

Wi-Fi signal strength is important for faster, more consistent data transfers. You can use PowerShell to gain a more ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two less than 9?

2020-12-28 12:10:20

james

Eric,
Is there a way to call these PowerShell commands from Word VBA. If so, how would you do it? (some examples are always very helpful).
P.S. Thank you for you many tips along the way. Wishing you a very happy new year!


Newest Tips