Get Folder Size from Windows Command Line Get Folder Size from Windows Command Line powershell powershell

Get Folder Size from Windows Command Line


There is a built-in Windows tool for that:

dir /s 'FolderName'

This will print a lot of unnecessary information but the end will be the folder size like this:

 Total Files Listed:       12468 File(s)    182,236,556 bytes

If you need to include hidden folders add /a.


You can just add up sizes recursively (the following is a batch file):

@echo offset size=0for /r %%x in (folder\*) do set /a size+=%%~zxecho %size% Bytes

However, this has several problems because cmd is limited to 32-bit signed integer arithmetic. So it will get sizes above 2 GiB wrong1. Furthermore it will likely count symlinks and junctions multiple times so it's at best an upper bound, not the true size (you'll have that problem with any tool, though).

An alternative is PowerShell:

Get-ChildItem -Recurse | Measure-Object -Sum Length

or shorter:

ls -r | measure -sum Length

If you want it prettier:

switch((ls -r|measure -sum Length).Sum) {  {$_ -gt 1GB} {    '{0:0.0} GiB' -f ($_/1GB)    break  }  {$_ -gt 1MB} {    '{0:0.0} MiB' -f ($_/1MB)    break  }  {$_ -gt 1KB} {    '{0:0.0} KiB' -f ($_/1KB)    break  }  default { "$_ bytes" }}

You can use this directly from cmd:

powershell -noprofile -command "ls -r|measure -sum Length"

1 I do have a partially-finished bignum library in batch files somewhere which at least gets arbitrary-precision integer addition right. I should really release it, I guess :-)


I suggest to download utility DU from the Sysinternals Suite provided by Microsoft at this linkhttp://technet.microsoft.com/en-us/sysinternals/bb896651

usage: du [-c] [-l <levels> | -n | -v] [-u] [-q] <directory>   -c     Print output as CSV.   -l     Specify subdirectory depth of information (default is all levels).   -n     Do not recurse.   -q     Quiet (no banner).   -u     Count each instance of a hardlinked file.   -v     Show size (in KB) of intermediate directories.C:\SysInternals>du -n d:\tempDu v1.4 - report directory disk usageCopyright (C) 2005-2011 Mark RussinovichSysinternals - www.sysinternals.comFiles:        26Directories:  14Size:         28.873.005 bytesSize on disk: 29.024.256 bytes

While you are at it, take a look at the other utilities. They are a life-saver for every Windows Professional