title: A PowerShell script to find the file size and file count of a folder with millions of files?
body: <p>The purpose of the script is the following:</p>
  
  <ol>
  <li>Print the number of files recursively found within a directory
  (omitting folders themselves)</li>
  <li>Print the total sum file size of the directory</li>
  <li>Not crash the computer because of massive memory use.</li>
  </ol>
  
  <p>So far (3) is the tough part.</p>
  
  <p>Here is what I have written and tested so far. This works perfectly well on folders with a hundred or even a thousand files:</p>
  
  <pre><code>$hostname=hostname
  $directory = "foo"
  $dteCurrentDate = Get-Date –f "yyyy/MM/dd"
  
  $FolderItems = Get-ChildItem $directory -recurse
  $Measurement = $FolderItems | Measure-Object -property length -sum
  $colitems = $FolderItems | measure-Object -property length -sum
  "$hostname;{0:N2}" -f ($colitems.sum / 1MB) + "MB;" + $Measurement.count + " files;" + "$dteCurrentDate"
  </code></pre>
  
  <p>On folders with millions of files, however, the <code>$colitems</code> variable becomes so massive from the collection of information of millions of files that it makes the system unstable. Is there a more efficient way to draw and store this information?</p>