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.
  • 58,865
  • 9
  • 159
  • 159
Elmex
  • 3,331
  • 7
  • 40
  • 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
  • 121,444
  • 32
  • 184
  • 206
3

Filter for files before counting:

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