Get Folder Size from Windows Command Line
Solution 1
You can just add up sizes recursively (the following is a batch file):
@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %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 :-)
Solution 2
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
.
Solution 3
Oneliner:
powershell -command "$fso = new-object -com Scripting.FileSystemObject; gci -Directory | select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName | sort Size -Descending | ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName"
Same but Powershell only:
$fso = new-object -com Scripting.FileSystemObject
gci -Directory `
| select @{l='Size'; e={$fso.GetFolder($_.FullName).Size}},FullName `
| sort Size -Descending `
| ft @{l='Size [MB]'; e={'{0:N2} ' -f ($_.Size / 1MB)}},FullName
This should produce the following result:
Size [MB] FullName
--------- --------
580,08 C:\my\Tools\mongo
434,65 C:\my\Tools\Cmder
421,64 C:\my\Tools\mingw64
247,10 C:\my\Tools\dotnet-rc4
218,12 C:\my\Tools\ResharperCLT
200,44 C:\my\Tools\git
156,07 C:\my\Tools\dotnet
140,67 C:\my\Tools\vscode
97,33 C:\my\Tools\apache-jmeter-3.1
54,39 C:\my\Tools\mongoadmin
47,89 C:\my\Tools\Python27
35,22 C:\my\Tools\robomongo
Solution 4
I suggest to download utility DU from the Sysinternals Suite provided by Microsoft at this link http://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:\temp
Du v1.4 - report directory disk usage
Copyright (C) 2005-2011 Mark Russinovich
Sysinternals - www.sysinternals.com
Files: 26
Directories: 14
Size: 28.873.005 bytes
Size 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
Solution 5
If you have git installed in your computer (getting more and more common) just open MINGW32 and type: du folder
Related videos on Youtube

Comments
-
Eldad Assis 6 months
Is it possible in Windows to get a folder's size from the command line without using any 3rd party tool?
I want the same result as you would get when right clicking the folder in the windows explorer → properties.
-
Rich about 10 yearsThat's hardly "without any 3rd-party tool", I guess.
-
Steve about 10 yearsOh right, but it thought that family should not be considered '3rd party'.
-
Rich about 10 yearsWell, granted, but it's still an additional download (or using
\\live.sysinternals.com
if that still exists). I wholeheartedly agree though, that all the sysinternals tools should be included by default. Although for many uses PowerShell is a quite worthy replacement. -
Rich about 10 yearsIf you just need
du
then Cygwin is way overkill. Just go with GnuWin32. -
Steve about 10 years+1 I was just wandering in the get-help * output to find something like that.
-
Illizian about 10 yearsIndeed it is overkill, but it's also awesome. +1 for your post above @joey (haven't got the rep to do it literally :( )
-
Rich about 10 yearsSteve, in my eyes PowerShell is way more Unix-y than Unix in that most core commands are really orthogonal. In Unix
du
gives directory size but all it's doing is walking the tree and summing up. Something that can be very elegantly expressed in a pipeline like here :-). So for PowerShell I usually look for how you can decompose the high-level goal into suitable lower-level operations. -
Rich about 10 yearsIn my humble opinion Cygwin may be useful to some but it's far from awesome and rather horrible. But I guess Unix users say the same about Wine.
-
Illizian about 10 years"wine" - shudder, I use Cygwin on my development laptop because the battery life is appalling under linux and it's not convenient to run a VM on it. It takes some setting up, but Cygwin is brilliant for those among us who miss the nix shell in Windows, personally I don't like Powershell I may have to check GnuWin out though. Thanks @Joey.
-
John Homer almost 10 yearsDid someone say Powershell? Try the following: "{0:N2}" -f ((Get-ChildItem C:\Temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB) + " MB"
-
garg over 9 yearsIt does not seem to return the current result for the home directory of a user. In one case, if I right click on a home folder and get properties, I see ~200 MB. If I do ls -r <dir path> | measure -s length then I see 6 megs (in bytes)
-
garg over 9 yearsI see, it seems to ignore hidden folders that way. ls -force -r works if I want to include hidden folders as well.
-
elady almost 9 yearsThanks, it works for me with folders which smaller than 1GB, but for example, I have a folder of 3,976,317,115 bytes (3.70GB), and the script return to me this string ") 932", do you know why?
-
Harry Johnston about 8 yearsFor reference, the Sysinternals du (per Steve's answer) is smart enough not to iterate into junction points, etc., and even handles hardlinks properly.
-
IT researcher over 7 years
folder\*
is not working for me the size of the folder that i put in the place shows 0 bytes, Is there any solution? -
Tatarin over 7 yearsThis is a bad and poor answer. What is the command line or code?
-
Linga almost 7 years@ITresearcher I'm also getting 0 as well. I tried
powershell -noprofile -command "ls -r|measure -s Length"
and works fine -
zumalifeguard over 6 yearsA slightly better looking version: powershell -noprofile -command "'{0:N0}' -f (ls -r|measure -s Length).Sum"
-
Oran D. Lord about 6 yearsThis is the most elegant solution if you don't require an integer return value. Note: the '/a' switch will include hidden and system files in the total size.
-
dkellner about 5 yearsI suggest you all to download whatever Mark Russinovich develops :) I keep seeing this name whenever I find a tool saving my life.
-
athos almost 5 yearsis there a way to add a command line parameter for the folder?
-
Rich almost 5 years@athos: Yes, there is.
-
Rich almost 5 years@athos: Use
%1
in place offolder
in the batch file, or add a parameter to the PowerShell script and add that as an argument toGet-ChildItem
. But perhaps such things are better asked as a separate question if you have trouble with that part (as it has no relation to the original question here). -
athos almost 5 yearsjust for those who might enter, these are the ways to query a folder: in powerscript:
Get-ChildItem -Path c:\temp -Recurse | Measure-Object -Sum Length
, incmd
:powershell -noprofile -command "'{0:N0}' -f (ls c:\temp -r|measure -s Length).Sum"
-
Disillusioned over 4 yearsThis utility (v1.61) has a permission problem resulting in the size of some files not being included. This makes the tool somewhat unreliable unless using an admin elevated console. NOTE: Files and sizes are listed with
dir
command. (I had thought it might be a file size issue due to the problem directory containing database files with many of them > 2GB.) -
Raunak Thomas over 4 yearsIs there a way to show all the files and size in Mb in the base folder as well? In this case `C:\my\Tools`
-
Raunak Thomas over 4 yearsPlus it breaks when there are lots of folders (For example in the C: drive)
-
David Ferenczy Rogožan about 4 yearsI guess it's not possible to print file sizes in some more useful units, like kB or MB, right?
-
andrew pate almost 4 yearsWorks for me. Useful if your on a headless server.
-
Alexander Anufriev almost 4 yearsNew version released! github.com/aleksaan/diskusage Improvments: 1) correct work with symlink's sizes 2) orevall info about analyzed objects
-
Tilo almost 4 years
0.00 C:\Windows
can I optimize this for all folder, tried already run as admin -
vard almost 4 yearsYou can use the following command
du -h -d 1 folder | sort -h
if you want human readable size, one level of subdirectories only, and sort by size. -
Sylwester Zarębski over 3 yearsIt will quit with error on first directory with no access. It should skip it, or give only warning.
-
julesverne over 3 yearsFor anyone worried Robocopy will move files.. don't. the /l option only lists. In addition, Robocopy is not 3rd party, Robocopy is installed in Windows by default from Vista up, XP you need to install it.
-
Eldad Assis over 3 yearsThanks for the addition. Glad to see this thread still attracting attention ;-) Might want to add how to install robocopy (great tool!).
-
sean.net over 3 yearsthis is good but it truncates my component folder names. what is the command to display them entirely?
-
frizik over 3 years@sean.net do you mean it ends with "..." in case of long folders? That's how
ft
(Format-Table
) works. You can tryfl
instead (Format-List
) -
Alexander Anufriev about 3 yearsVersion 2.0.2 released! github.com/aleksaan/diskusage/releases/tag/v2.0.2 - Configuration file instead inline parameters - Simple usage mode (copy diskusage executable to any folder, run it and get results) - Correct results output (correct end of line symbol win/unix) - Save results to file or get it in console mode (option 'tofile')
-
curropar about 3 yearsSimple and elegant! My version:
$FolderList = Get-ChildItem -Directory -Force; $data = foreach ($folder in $FolderList) { set-location $folder.FullName; $size = Get-ChildItem -Recurse -Force | Measure-Object -Sum Length; $size | select @{n="Path"; e={$folder.Fullname}}, @{n="FileCount"; e={$_.count}}, @{n="Size"; e={$_.Sum}} }
This will provide objects (more Powershell-ish), 'FileCount' and 'Size' are integers (so you can process them later, if needed), and 'Size' is in bytes, but you could easily convert it to GB (or the unit you want) with$Data.Size / 1GB
. -
curropar about 3 yearsOr a one-liner:
$Data = dir -Directory -Force | %{ $CurrentPath = $_.FullName; Get-ChildItem $CurrentPath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Sum Length | select @{n="Path"; e={$CurrentPath}}, @{n="FileCount"; e={$_.count}}, @{n="Size"; e={$_.Sum}} }
-
Intrastellar Explorer about 3 yearsI found I was hitting a bunch of errors with very deep file structures, and then learned of Windows
260
char path limit. Changing registry to allow long file paths fixed this for me. howtogeek.com/266621/… -
user2924019 over 2 yearsDouble quotes instead of single quotes.
-
aeracode over 2 yearsPlease, make note that you are an author. (From stackoverflow.com/help/promotion: "However, you must disclose your affiliation in your answers.")
-
BenderBoy over 2 yearsI like this one best, but isn't the
set-location
unnecessary? You can just goGet-ChildItem $folder.FullName
, that way you come out the other side without the current directory changing underneath you. -
Ryan Lee over 2 yearsThanks @BenderBoy. It's better to remove
set-location
and useGet-ChildItem $folder.FullName
directly. -
Serge N about 2 years@ITresearcher The format of the
for
command is incorrect, it should be this:for /r folder %%x in (*) do set /a size+=%%~zx
-
Alexander Anufriev about 2 yearsYes, I am author of github.com/aleksaan/diskusage program
-
Alexander Anufriev about 2 yearsVersion 2.1.0 has been released. github.com/aleksaan/diskusage/releases/tag/v2.1.0 - memory consumpion optimization - reducing time of analysis - bug fixing
-
Alexander Anufriev about 2 yearsVersion 2.2.0 has been released.github.com/aleksaan/diskusage/releases/tag/v2.2.0 - more 5 times faster now!
-
jeyko almost 2 yearsonly works if i rearrange the command here -
dir 'FolderName'/s
-
Eldad Assis 9 monthsRight, but this is not windows native in 2012 ;-) +1 anyway for the idea
-
user1016274 8 monthsI posted the original solution 4 years earlier here on SO. Using
robocopy
by far is the fastest solution. -
Admin 8 monthsthis gives the size of every file - it's still spinning after 5 minutes when the powershell script gave me the totals in about 2 seconds
-
Alexander Anufriev 8 monthsVersion 2.8.0 github.com/aleksaan/diskusage was released. Duck has been born!