title: Listing Folder Size by Piping
body: <p>How can I list size of each folder in a directory by the sum of all files in each folder/subfolders?</p>
  
  <p>My latest attempt:</p>
  
  <pre><code>ls | foreach-object { select-object Name, @{Name = "Size"; Expression = { ls $_ -recurse | measure-object -property length -sum } }
  </code></pre>
  
  <p>I've made other attempts but nothing successful yet. Any suggestions or solutions are very welcome. I feel like I'm missing something very obvious.</p>
  
  <p>The output should look as follows:</p>
  
  <p>Name             Size</p>
  
  <p>And it should list each folder in the root folder and the size of the folder counting subfolders of that folder.</p>
  
  <p>I was able to resolve the issue with the following:</p>
  
  <pre><code>param([String]$path)
  ls $path | Add-Member -Force -Passthru -Type ScriptProperty -Name Size -Value {  
     ls $path\$this -recurse | Measure -Sum Length | Select-Object -Expand Sum } | 
     Select-Object Name, @{Name = "Size(MB)"; Expression = {"{0:N0}" -f ($_.Size / 1Mb)} } | sort "Size(MB)" -descending
  </code></pre>