1

I'm recursively counting total number of objects (files, folders, etc) to check folders vs their Amazon S3 backups.
When I use windows explorer on a folder (right click --> properties), I get a smaller number of total objects than what the following powershell code generates. Why?
Amazon S3 matches the count from Windows Explorer 100% of the time. Why is powershell giving a higher total number, and what is the likely difference (system files, hidden files, etc)? The total number of objects in these folders is routinely 77,000+.

folder_name; Get-ChildItem -Recurse | Measure-Object | %{$_.count}

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
Jordan
  • 11
  • 3

3 Answers3

1

To count the number of files and folders in separate variables, you can do

# create two variables for the count
[int64]$totalFolders, [int64]$totalFiles = 0
# loop over the folders in the path
Get-ChildItem -Path 'ThePath' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
    if ($_.PSIsContainer) { $totalFolders++ } else { $totalFiles++ }
}
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"

The -Force switch makes sure also hidden and system files are counted.

A probably faster alternative is to use robocopy:

$roboCount    = robocopy 'ThePath' 'NoDestination' /L /E /BYTES
$totalFolders = @($roboCount -match 'New Dir').Count - 1   # the rootfolder is also counted
$totalFiles   = @($roboCount -match 'New File').Count
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
Theo
  • 57,719
  • 8
  • 24
  • 41
  • 1
    Thank you for `-Force` PowerShell was ignoring items flagged as hidden resulting in missed asset processing and asset counts. – Reahreic Mar 03 '23 at 14:32
0

I was unable to replicate.

When in file explorer, right-click the folder in question -> properties Under the General tab, there's a section called Contains. This lists both the Files and Folders as separate numbers. In my example I have 19,267 Files, 1,163 Folders which is a total of 20,430 objects

When I run

Get-ChildItem -Path C:\folder -Recurse | measure | % Count

it returns 20430

When I run

Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $false} | measure | % count

it returns 19267

When I run

Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $true} | measure | % count

it returns 1163

Are you certain that you're counting both the files and folders when manually viewing the properties?

PowerShellGuy
  • 733
  • 2
  • 8
0

The discrepancy comes from Windows Explorer counting files, and separately, folders. Powershell (version 2.0 Build 6.1) is counting everything together. It seems -File and -Directory don't work in PowerShell V2.0.

I really want to be able to get a list as a .cvs or .txt output of just the number of files (recursively) from a large number of folders. Going through windows explorer is one by one, and I don't get this as an output that I can copy/paste.

Jordan
  • 11
  • 3
  • Filter directories using [PSIsContainer](https://stackoverflow.com/a/3085524/52598). Note that the linked answer only wants folders so invert the condition. – Lieven Keersmaekers Oct 09 '20 at 06:06