Counting the Number of Files or Subfolders Using PowerShell

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

Clipboard History

Windows has had cut and paste functionality for years. Windows 10 improves the Clipboard functionality by including a new ...

Discover More

Use Cmd to Find Your Computer Serial Number

You can access your computer's serial number without looking all over on the outside of the box. Use the Command prompt ...

Discover More

Typing Insights

Gaining insights into how you type can improve your typing skills and allow you to see how you're doing. The Windows 10 ...

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

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

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

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 four less than 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
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.