Skip to content
Home » security » Revisiting Microsoft/Sysinternals Du as a batch file

Revisiting Microsoft/Sysinternals Du as a batch file

My tips for using Sysinternals’ Du.exe were well received last week, and my former coworker Charlie mentioned a GUI tool called Windirstat that I had completely forgotten about. For the command-line averse, it’s an incredibly useful tool.

But there’s one thing that Du.exe does that makes the CLI worthwhile. It will output to CSV files for further analysis. Here’s the trick.

DU -L 1 -Q -C \\SERVERNAME\C$\ >> servers.csv

Sub in the name of your server for servername. You have to have admin rights on the server to run this, of course.

For even more power, run this in a batch file containing multiple commands to query multiple servers, say, in your runup to Patch Tuesday. Open the file in your favorite spreadsheet, sort on Directory Size, and you can find candidates for cleanup.

Querying lots of remote servers can take time but you can run it in the background as you do other work, and/or overnight, then dig in to the largest directories to isolate the biggest space hogs. Something like this works just fine:

DU -L 1 -Q -C \\STLMS01\C$\ >> servers.csv
DU -L 1 -Q -C \\STLMS02\C$\ >> servers.csv
DU -L 1 -Q -C \\STLMS03\C$\ >> servers.csv
DU -L 1 -Q -C \\STLMS04\C$\ >> servers.csv
DU -L 1 -Q -C \\STLMS05\C$\ >> servers.csv

Or, for more elegance, use a for loop.

for %%i in (stlms01 stlms02 stlms03 stlms04 stlms05) do DU -L 1 -Q -C \\%%i\c$\ >> servers.csv

Save it as a batch file (with a .bat or .cmd extension), run it over the weekend if need be, then examine the file servers.csv when it finishes. You’ll have to sort the file and delete the duplicate header lines, but that’s easy to do. And once you have the data, it’s not terribly difficult to act on it.

There were times I doubled the productivity of server teams by using simple batch files with for loops to automate repetitive work like this. The number of people who can do something like this is frighteningly small, so this is definitely a skill worth having. I heard stories for years that ended with the line, “Dave didn’t have any trouble doing that.”

A lot of what I was doing was a simple for loop from a command line. Here are some more scripting sysadmin tips.

If you found this post informative or helpful, please share it!
%d bloggers like this: