12

How can I count all files in a specific folder (and all subfolders) with the Powershell command Get-ChildItem? With (Get-ChildItem <Folder> -recurse).Count also the folders are counted and this is not that what I want. Are there other possibilities for counting files in very big folders quickly?

Does anybody know a short and good tutorial regarding the Windows Powerhell?

CB.
  • 56,179
  • 8
  • 151
  • 155
Elmex
  • 3,291
  • 7
  • 38
  • 64
  • 1
    for the tutorial you can start from here: http://powershell.com/cs/blogs/ebook/ – CB. Nov 23 '11 at 10:01

2 Answers2

19

I would pipe the result to the Measure-Object cmdlet. Using (...).Count can yield nothing in case there are no objects that match your criteria.

 $files = Get-ChildItem <Folder> -Recurse | Where-Object {!$_.PSIsContainer} | Measure-Object
 $files.Count

In PowerShell v3 we can do the following to get files only:

 Get-ChildItem <Folder> -File -Recurse
Shay Levy
  • 114,369
  • 30
  • 175
  • 198
3

Filter for files before counting:

(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count
jon Z
  • 14,930
  • 1
  • 31
  • 35