du -h -s but for Windows

du -h -s but for Windows

As you know the du command in Linux is amazingly powerful, it is a quick and easy way to dig into a directory and get content size and more. Well personally one of the biggest things I have with Windows is from the PowerShell you can’t really do this….. Easily.

Lets take a look at a few things first, I want a directory listing in Linux I just simply run the ls -lah and this will show the output of the contents in said folder,

Great so I can do that in Windows, its called the dir command.

Ok, but how big is that folder? How much stuff is inside of it? Example how big is the Downloads Folder? Well hmm, a dir does spit out some information, but I still have to math, which makes my brain hurt.

Ok so cool, but I can do that in Linux no problems. Linux allows us two, well many different options, the biggest two that I use, and I will use the above example, is the du -h -s /home/torresoc/ and the du -h -s /home/torresoc/* And look at the difference.

Now this allows you to start digging down into the folders and in this example, the user torresoc we can note that the mail folder is where all the data is being held at. This is great for helping to find who is hording all the storage.

Now in Windows ** Again we are working from the command line/powershell not a gui where you can just right click on something, and then wait, and right click and wait, and so forth.

Ok so you made your point, now how do I do the same thing in powershell?

This took me some time to find and hack together, but it works, now the big part it has to be on the different lines when you past it into the prompt. Currently it is not a one line command.

gci -force “C:\Users”-ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$_.fullname, ‘{0:N2} GB’ -f ($len / 1Gb)
}

So the above is a great way to show you the users and their disk usage.

Great! In this example ** Please note that this would be more used in a server environment and I am using my desktop as the example, so the Users folder is well empty but me. You can see that the user atorr is using 17.01 GB of disk space. Great! Now we can go dig even further into this and find even more.

Lets change from C:\Users to C:\Users\atorr

Cool so now we can see tha this user has 4.33 GB of data in the Downloads folder. Ok so if this was in an office environment we could dig even deeper into this users folder and see whats what. This is a great script that I have learned and use almost every single day now that I am working back in the Windows server world.

*** UPDATE ***

So I figured out how to do this on one line so we now don’t have to worry about the line breaks.

gci -force "C:\Users\hw" -ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % { $len = 0; gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }; $_.fullname, '{0:N2} GB' -f ($len / 1Gb) }

Now copy that and paste it, and it will spit out the same results, but now not worrying about the line breaks!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *