Using du.exe (Sysinternals) is it possible to show folders above a certain size?

du.exe lets you recursively identify folders that take up a large amount of space. For example, the following will show you the size of all the folders from c:\ 3-levels deep:

du.exe -l 3 c:\

How can I filter this output to only show me the folders above 1GB?

Here's an extract of the output from du.exe for reference:

Du v1.4 - report directory disk usage
Copyright (C) 2005-2011 Mark Russinovich
Sysinternals - www.sysinternals.com

   6,344,864  c:\Windows\winsxs
  18,268,671  c:\Windows
 483,343,308  c:\
Files:        412125
Directories:  42072
Size:         494,943,548,281 bytes
Size on disk: 487,560,269,896 bytes

Solution 1:

I'm providing a new variant on the previous du / powershell combos, just because neither of them worked for me with du v1.61, and others may face the same issue. I ran into a few problems with the previous variants:

  1. du's -nobanner switch needs to be included (and wasn't) so that ConvertFrom-Csv doesn't choke on the banner at the top (for me, manifesting as mysterious invisible output)
  2. Even then, powershell would return e.g. Cannot convert value "40822284152" to type "System.Int32". Error: "Value was either too large or too small for an Int32." for some lines; presumably very large directories.
  3. Lastly, the output would include very small directories, well below 1GB, despite the presence of Where-Object { [int]$_.DirectorySize -gt 1048576 }

I'm not a powershell expert, but I've managed to adapt the previous two variants to provide a working solution:

du -nobanner -c -l 3 \ | ConvertFrom-Csv | select Path,@{Name="DirectorySize";expression={$_.DirectorySize / 1GB }} | Where-Object { $_.DirectorySize -gt 1 } | Sort-Object { $_.DirectorySize } -descending

And just for giggles, here's a less flexible / less precise solution using du and findstr with a regular expression:

du -l 3 \ | findstr /R /C:",[0-9][0-9][0-9],[0-9][0-9][0-9]  "

This takes advantage of the pattern that directories over 1GB will be printed in. Specifically there will be an extra leading comma before the three digits, and then another comma followed by three more digits. The trailing two spaces are required to filter out the totals that are printed at the bottom. Note: this solution doesn't sort by directory size as with the powershell solution.

Solution 2:

As the other answer says, you can't do it with du.exe alone. PowerShell to the rescue!

.\du.exe -c -l 3 C:\ | ConvertFrom-Csv -Header Size,Path | Where-Object { [int]$_.Size -gt 1048576 } | Sort-Object { [int]$_.Size } -descending

Explanation

Breaking that long command down into the individual bits that are each piped into the next one:

.\du.exe -c -l 3 C:\

This is basically what you started with, except the -c parameter tells du to format the output as CSV.

ConvertFrom-Csv -Header Size,Path

This takes the CSV output from du and converts it into a PowerShell hashtable. Since du doesn't provide a header with column names, that has to be done manually.

Where-Object { [int]$_.Size -gt 1048576 }

This filters the data, returning only those rows where the size is greater than 1 GB (du returns sizes in KB, and 1 GB = 1048576 KB). Note the [int] part, to let PowerShell know that it's dealing with numeric data.

Sort-Object { [int]$_.Size } -descending

This sorts the data by size, in descending order (again specifying that the data to sort by is numeric). This is optional, of course.