Get Folder Size from Windows Command Line

650,925

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

Share:
650,925

Related videos on Youtube

Eldad Assis
Author by

Eldad Assis

Solving real life DevOps problems...

Updated on July 08, 2022

Comments

  • Eldad Assis
    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
    Rich about 10 years
    That's hardly "without any 3rd-party tool", I guess.
  • Steve
    Steve about 10 years
    Oh right, but it thought that family should not be considered '3rd party'.
  • Rich
    Rich about 10 years
    Well, 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
    Rich about 10 years
    If you just need du then Cygwin is way overkill. Just go with GnuWin32.
  • Steve
    Steve about 10 years
    +1 I was just wandering in the get-help * output to find something like that.
  • Illizian
    Illizian about 10 years
    Indeed it is overkill, but it's also awesome. +1 for your post above @joey (haven't got the rep to do it literally :( )
  • Rich
    Rich about 10 years
    Steve, 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
    Rich about 10 years
    In 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
    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
    John Homer almost 10 years
    Did someone say Powershell? Try the following: "{0:N2}" -f ((Get-ChildItem C:\Temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB) + " MB"
  • garg
    garg over 9 years
    It 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
    garg over 9 years
    I see, it seems to ignore hidden folders that way. ls -force -r works if I want to include hidden folders as well.
  • elady
    elady almost 9 years
    Thanks, 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
    Harry Johnston about 8 years
    For 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
    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
    Tatarin over 7 years
    This is a bad and poor answer. What is the command line or code?
  • Linga
    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
    zumalifeguard over 6 years
    A slightly better looking version: powershell -noprofile -command "'{0:N0}' -f (ls -r|measure -s Length).Sum"
  • Oran D. Lord
    Oran D. Lord about 6 years
    This 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
    dkellner about 5 years
    I suggest you all to download whatever Mark Russinovich develops :) I keep seeing this name whenever I find a tool saving my life.
  • athos
    athos almost 5 years
    is there a way to add a command line parameter for the folder?
  • Rich
    Rich almost 5 years
    @athos: Yes, there is.
  • Rich
    Rich almost 5 years
    @athos: Use %1 in place of folder in the batch file, or add a parameter to the PowerShell script and add that as an argument to Get-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
    athos almost 5 years
    just 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, in cmd: powershell -noprofile -command "'{0:N0}' -f (ls c:\temp -r|measure -s Length).Sum"
  • Disillusioned
    Disillusioned over 4 years
    This 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
    Raunak Thomas over 4 years
    Is 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
    Raunak Thomas over 4 years
    Plus it breaks when there are lots of folders (For example in the C: drive)
  • David Ferenczy Rogožan
    David Ferenczy Rogožan about 4 years
    I guess it's not possible to print file sizes in some more useful units, like kB or MB, right?
  • andrew pate
    andrew pate almost 4 years
    Works for me. Useful if your on a headless server.
  • Alexander Anufriev almost 4 years
    New version released! github.com/aleksaan/diskusage Improvments: 1) correct work with symlink's sizes 2) orevall info about analyzed objects
  • Tilo
    Tilo almost 4 years
    0.00 C:\Windows can I optimize this for all folder, tried already run as admin
  • vard
    vard almost 4 years
    You 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
    Sylwester Zarębski over 3 years
    It will quit with error on first directory with no access. It should skip it, or give only warning.
  • julesverne
    julesverne over 3 years
    For 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
    Eldad Assis over 3 years
    Thanks for the addition. Glad to see this thread still attracting attention ;-) Might want to add how to install robocopy (great tool!).
  • sean.net
    sean.net over 3 years
    this is good but it truncates my component folder names. what is the command to display them entirely?
  • frizik
    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 try fl instead (Format-List)
  • Alexander Anufriev about 3 years
    Version 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
    curropar about 3 years
    Simple 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
    curropar about 3 years
    Or 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
    Intrastellar Explorer about 3 years
    I 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
    user2924019 over 2 years
    Double quotes instead of single quotes.
  • aeracode
    aeracode over 2 years
    Please, make note that you are an author. (From stackoverflow.com/help/promotion: "However, you must disclose your affiliation in your answers.")
  • BenderBoy
    BenderBoy over 2 years
    I like this one best, but isn't the set-location unnecessary? You can just go Get-ChildItem $folder.FullName, that way you come out the other side without the current directory changing underneath you.
  • Ryan Lee over 2 years
    Thanks @BenderBoy. It's better to remove set-location and use Get-ChildItem $folder.FullName directly.
  • Serge N
    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 years
    Yes, I am author of github.com/aleksaan/diskusage program
  • Alexander Anufriev about 2 years
    Version 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 years
    Version 2.2.0 has been released.github.com/aleksaan/diskusage/releases/tag/v2.2.0 - more 5 times faster now!
  • jeyko
    jeyko almost 2 years
    only works if i rearrange the command here - dir 'FolderName'/s
  • Eldad Assis
    Eldad Assis 9 months
    Right, but this is not windows native in 2012 ;-) +1 anyway for the idea
  • user1016274
    user1016274 8 months
    I posted the original solution 4 years earlier here on SO. Using robocopy by far is the fastest solution.
  • Admin
    Admin 8 months
    this 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 months
    Version 2.8.0 github.com/aleksaan/diskusage was released. Duck has been born!