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

Keeping Track Of Your Timeline

Sometimes you need to go back and revisit an activity you were doing from a couple of days ago. With the new Timeline ...

Discover More

Quick Rename of Files in Bulk

Renaming a large set of files one file at a time can be time consuming and tedious. Windows allows for you to quickly ...

Discover More

Review and Clear Bing Search History

Search engines are a great way to gather information about the world around us. While a search engine presents ...

Discover More
More WindowsTips

Check your PC's Uptime with PowerShell

Every time a computer restarts, it runs through processes that help its performance. You can use PowerShell to see how ...

Discover More

Use PowerShell to Find Your Computer Serial Number

When you need your computer's serial number you can look it up without looking on the outside of the box. Use PowerShell ...

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 4 + 5?

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