Elmex
Elmex

Reputation: 3361

File counting with Powershell commands

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?

Upvotes: 12

Views: 24599

Answers (2)

Shay Levy
Shay Levy

Reputation: 126902

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

Upvotes: 19

jon Z
jon Z

Reputation: 16646

Filter for files before counting:

(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count

Upvotes: 3

Related Questions