Reputation: 3361
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
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
Reputation: 16646
Filter for files before counting:
(Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count
Upvotes: 3