If you want to know the total size of all files in a particular folder, you can easily do this in Explorer. However, if you need distinctive information like the size of a certain file type, PowerShell gives you more options. Measure-Object and Get-ChildItem are usually the tools of choice for the task.

At a command prompt, you can determine directory sizes with this command:

But if you want to list file sizes in directory trees, this method won't work, and you will need tools such as Sysinternals disk usage (du).

In PowerShell, Get-ChildItem (alias gci) and Measure-Object usually solve the problem in combination. The first cmdlet retrieves the file lists based on various filters, and the second one does the computation. The PowerShell counterpart of the above example looks like this:

You have to pass the attribute that Measure-Object is supposed to use for the calculation with the Property parameter. Here, an object of the type System.IO.FileInfo will give you the file size via the property Length. You can omit the parameter in short notation:

Displaying the total file size with Measure Object

Displaying the total file size with Measure Object

As you can easily see in the result, the Count property contains the number of files. A big number behind Sum shows the total file sizes. Obviously, the unit is bytes, which you can convert to MB or GB:

Converting file sizes to GB and processing the output with the format operator

The result will have quite a few decimal places. To round them to two, the format operator is useful:

Of course, with the help of a filter you can limit the output to certain file types:

This command tells you how much space ISO files occupy in the Downloads folder.

It gets more interesting if you want to calculate the size of all subdirectories:

Computing the size of subdirectories with gci and measure

Computing the size of subdirectories with gci and measure

Here, the first command collects the names of all folders in the current directory and pipes the result to the loop (alias "%"). The loop first displays the directory name and then calls Get-ChildItem again with the File parameter to pass the contents of the corresponding folder to Measure-Object.

By the way, to find only empty subdirectories, you don't need Measure-Object. You can just use the result of the GetFiles() method:

In addition to the sum, Measure-Object also processes other values such as the average, the minimum, and the maximum:

However, in the context of file sizes, these parameters are only of limited use. For instance, Measure-Object does not give you the name of the largest or smallest files. If you want to identify the biggest storage hogs, you will need Sort-Object.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

3+

Users who have LIKED this post:

  • avatar
Share
15 Comments
  1. Wolfgang,

    These are great posts, but you are making one mistake that I think is pretty bad. You are using aliases and abbreviated cmdlet names in your code, which is perfectly acceptable for day to day usage but not a best practice when sharing code. I shorten my cmdlets all day long, but I don't shorten cmdlets or uses aliases when explaining to others or sharing scripts.

    For example, a new user getting started with Powershell will not know that % is shorthand for foreach-object. I wasted a full day chasing this alias down one day when I first started with PS because I didn't know about aliases or how to look them up.

    These are solid articles otherwise! Nice job.

    19+

    Users who have LIKED this comment:

    • avatar
  2. Wolfgang Sommergut 2 years ago

    Mike,

    thank for your feedback, good point! I have assumed that these commands are being used interactively. In a script, aliases should not be used since you cannot be sure they do exist on a target machine. But you are right, from a didactic perspective I should explain an alias before using it first.

    Wolfgang

    2+

    Users who have LIKED this comment:

    • avatar
  3. Mike, you are right, aliases shouldn't be used in scripts that you share. It is also best practice not to use aliases in scripts that you don't share even if the aliases are common and available everywhere because if you have to review your own script in a year or two, it will be much easier to read.

    I am also often using the cmdlet names in blog posts even in interactive scenarios because these commands are easier to understand for beginners. You usually can assume that the reader will never really type this command and just copy and paste it. 

    However, writing a blog post where you explain how certain tasks can be accomplished interactively is a completely scenario. The reason why aliases exist is because cmdlet names can be pretty long-winded. This improves readability but also means a lot of type-type compared to other popular shells such as bash. Tab completion can help here, but it will still slow you down significantly. I mean, who really types "Get-Childitem" on a console to view the contents of a folder?

    Thus, if you are teaching how to work on a console, aliases are the main players because this is what learners are supposed to actually use. Of course, you should mention the corresponding cmdlet names in the text and this is what Wolfgang is usually doing.

    2+

  4. Ian 2 years ago

    Really good explanation of how to calculate folder size. I am always having to find this out for other people so will now script it and let them run it when they want to. This is going to save me time. Thanks.

     

    1+

  5. Hi, I was just trying to get total numbers of files in each folder recursively. I tried

    $folders = get-childitem -recurse "mypath"
    $folders |
    foreach { $_.FullName; (get-childitem -File $_.FullName | Measure-Object) }

    It worked but then it also returned counts (always just 1) for each of the text files in each subfolder. Is there any way to just limit it to the numbers of files in the directories?

    2+

  6. JillJ 1 month ago

    None of the file-counts or file-sizes are correct using PowerShell.

    They *NEVER* match using File Explorer >> Right-click >> Properties.

    Not even close.

    0

  7. @JillJ I would trust the Powershell numbers over the File Explorer numbers any day.  Explorer does all kinds of rounding, estimating, etc. 

    However, PS won't necessarily pick up hidden files, and system files, etc. But - it also doesn't pick up files you don't have access to.   So, a lot depends on exactly what you are looking for.  Another important thing is *how* you gather them. 

    @Kim Vogel: You just need a couple of minor changes..

    You need to specify -directory on the first line to avoid picking up files.

    And using the [pscustomobject] lets you gather the info together and present it cleanly.  

    1+

  8. Cornel H 4 weeks ago

    David, easy to understand and very well explained with examples. Thanks!

    How can i export the results from a [pscustomobject] to a log file ( ie ExportCs,etc. Export-Csv prompt me for an InputObject)

    0

  9. Cornel H 4 weeks ago

    David, easy to understand and very well explained with examples. Thanks!

    How can i export the results from an [pscustomobject] to a log file ( ie ExportCs,etc. Export-Csv prompt me for an InputObject)

    0

  10. You have it correct Cornel.. on line 9, you just add to it.. 

    David F. 

    1+

  11. Cornel H 3 weeks ago

    That worked great! Thank you.

    0

  12. Larry Wakeman 2 weeks ago

    Hi, I just started using PowerShell today (though I'm a seasoned programmer).  What I'm trying to do is sum file sizes for specific file types, like *.msp.  Thanks!

    0

  13. @Larry Wakeman,

    All you have to do is filter your Get-ChildItem, grab the length, and sum it up..

    You can shorten it up and remove the -file and the -filter

    or.. we can go overboard and turn it into a full blown function..

    I'm showing all kinds of things you *can* do.. 

    David F. 

    0

  14. Jay Montana 5 days ago

    Hello,

    This is very helpful but and I am trying to adapt it to what I need but stuck.

    I need the run through a list of shares (UNC paths) and report the size of each share and how many files.  I need this for all folders and subfolders and files but I don't need a breakdown of the subfolders and files.  I do not need hidden files.

    So for example, these 3 shares.  I need import a csv list of shares / paths.

    \\Server1\UserShare1\Bob

    \\Server2\UserShare2\John

    \\Server3\UserShare3\Amy

     

    thanks!

    0

  15. Are you on the inside or the outside? (i.e. calculating from the server itself, or from a workstation)

    You can just use the function I wrote out and use the extension as .*, and we can add a tracker to the function.. 

    That adjusted version should get you what you are looking for.  Just feed in your list of paths, and that should get you the data 🙂

    David F.

    0

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account