Networking HowTos
Networking HowTos

Display Total Folder Size with PowerShell and Sort by Size

July 12, 2012 Microsoft, PowerShell

The PowerShell command below can be used to display a list containing the total size (in MB) of all the files and folders (recursively) in the current directory, and sort them by total size.

ls -Force | Add-Member -Force -Passthru -Type ScriptProperty -Name Length -Value {ls $this -Recurse -Force | Measure -Sum Length | Select -Expand Sum } | Sort-Object Length -Descending | Format-Table @{label="TotalSize (MB)";expression={[Math]::Truncate($_.Length / 1MB)};width=14}, @{label="Mode";expression={$_.Mode};width=8}, Name

Sample Output:

TotalSize (MB) Mode     Name
-------------- ----     ----
         26499 d----    Windows
         18409 d----    restore
          8189 -a-hs    pagefile.sys
          7989 d-r--    Users
          7672 d-r--    Program Files (x86)
          6141 -a-hs    hiberfil.sys
          2160 d-r--    Program Files
           235 d----    eclipse
           204 d----    NVIDIA
            66 d--h-    ProgramData
            37 d----    PHP
             0 d--hs    $Recycle.Bin
               d--hs    Recovery
               d--hs    System Volume Information
               d----    PerfLogs
               d--hs    Documents and Settings

This may take awhile to run if you run it from a folder with a lot of sub folders/files.

You Might Also Like

  • Bob March 19, 2013 at 10:56 pm

    Does not work in Powershell on Windows Server 2012 only get directory listing