23

I would like to see sizes of subfolders in a folder, similar to linux du -sh command. How can I list directories and their sizes in command prompt?

3

3 Answers 3

31

Try the Disk Usage utility from Sysinternals. Specifically, du -l 1 should show the size of each subdirectory of the current directory. For more information, run du without any parameters.


If PowerShell is OK, then try the following:

Get-ChildItem |
Where-Object { $_.PSIsContainer } |
ForEach-Object {
  $_.Name + ": " + (
    Get-ChildItem $_ -Recurse |
    Measure-Object Length -Sum -ErrorAction SilentlyContinue
  ).Sum
}

The sizes are in bytes. To format them in some larger unit like MB, try the following (condensed to one line):

Get-ChildItem | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.Name + ": " + "{0:N2}" -f ((Get-ChildItem $_ -Recurse | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) + " MB" }

For more information, see this article at Technet.

If you want more flexible formatting of the sizes (choosing kB/MB/GB/etc based on the actual size), see this question and its answers.


I don't think it's possible to do what you want from the regular command line and with only a few simple commands. See this script as an example (not going to copy it here because I don't believe that approach is worth pursuing, unless PowerShell isn't available and third-party utilities aren't acceptable).

3
  • Looks good. Will wait a bit to see if there is a native way to do this in command prompt :)
    – giorgio79
    Sep 29, 2012 at 11:47
  • @giorgio79 See updated answer. It's doable natively in the command prompt, but I'd recommend either Disk Usage, or using PowerShell.
    – Indrek
    Sep 29, 2012 at 12:19
  • Great PS one-liner - perfect when you can't deploy any new tools.
    – holtavolt
    Oct 19, 2020 at 15:44
0

I have no experience with du in Linux. But in windows I use dir /s to list all folders and subfolders along with file sizes.

3
  • 2
    The /S switch doesn't show sizes of directories, it simply makes the command recursively go through all subdirectories.
    – Indrek
    Sep 29, 2012 at 12:07
  • 3
    Look carefully, It shows the size of each folder also. Although its not much human readable.
    – Ankit
    Sep 29, 2012 at 12:28
  • Ah yes, I see what you mean - under the file listing of each subdirectory it shows the total size of all files in that directory. Like you say, it's not really easy to read, though, and the sizes aren't shown recursively.
    – Indrek
    Sep 29, 2012 at 12:58
0

This is a modification to Indrek's answer. I added the followings:

  • made it recursive to go further in subdirectories
  • added a size threshold to omit samller directories
  • replaced directory names with their relative path from current directory
  • sorted results by their path (I found it more convinient rather than sorting them by size, when they are already filtered by size)
  • added some formatting to make the results more readable

So, set $minsizeMB and $maxdepth with the desired values and run the script.

$minsizeMB = 100; 
$maxdepth = 5; 
Get-ChildItem -depth $maxdepth -Recurse |
    Where-Object { $_.PSIsContainer } |
    Select-Object FullName | 
    Sort-Object FullName |
    ForEach-Object { 
        $directory = Get-Item $_.FullName;
        $path = Resolve-Path -Relative $directory.FullName ; 
        $size = ((Get-ChildItem $directory -Recurse | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) ; 
        if ($size -ge $minsizeMB) { "{0,-90}: {1:N2} MB" -f ($path, $size) } 
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .