title: Item counts and sizes within a folder structure
body: <p>Counting and measuring folder content</p>
  
  <p>Trying to script check for large folders in within a directory structure. </p>
  
  <p>My test script only seem to work accurately on top level folder and only with two separate Get-ChildItem commands, one to provide count the other the sum.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>
  $profiles = Get-ChildItem "C:\Users"
  
  ForEach ($user in $profiles) {
  
  &lt;# getting the total number of items in the folder first #&gt;
  $itemsInfo1 = Get-ChildItem "C:\Users\$user" -Recurse -Force -ErrorAction SilentlyContinue 
  $allitemsCount = $itemsInfo1.Count
  
  &lt;# getting the total size of all items in the folder second #&gt;
  $itemsInfo2 = Get-ChildItem "C:\Users\$user" -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue
  $allitemsSize = $itemsInfo2.Sum
  
  $itemsSizeMB = [System.Math]::Round((($allitemsSize)/1MB),2) 
  $itemsSizeGB = [System.Math]::Round((($allitemsSize)/1GB),2) 
  
  Write-Host $user,$allitemsCount" Items",$itemsSizeMB" MB", $itemsSizeGB" GB"
  
  </code></pre>
  
  <p>I wanted to peruse this script on any folder by simple manual overwriting of the path in $profiles and $itemsInfo variables. </p>
  
  <p>However changing the relevant paths by one level lower subfolder (e.g. $profiles = C:\Users\John) to see list of subfolders from there returns sum sizes as 0, count works ok.</p>
  
  <p>Also contrary to my expectation the scrip only seems to return correct values when I use two separate Get-ChildItem commands as the .Sum and .Count properties returned by the two Get-ChildItem differ. </p>
  
  <p>First Get-ChildItem (i.e. $itemsInfo1) seems to return correct count of items in the folder but incorrect sum of the sizes.
  Second Get-ChildItem (i.e. $itemsInfo2) seems to return incorrect count of items (only counts files not folders?) in the folder but correct(er) sum of the sizes.</p>
  
  <p>I used the GUI > folder properties to establish what a "correct" count and size should be.</p>