2012 Scripting Games Advanced Event 4: Determine Folder Space

 2012 Scripting Games badge

Summary: In Advanced Event 4, you are required to determine which folders consume the most space.


About this event



Date of Event

4/5/2012 12:01 AM

Due Date

4/12/2012 12:01 AM


Event scenario

You are a freelance journalist, and as a result, you travel to various locations around the world in search of the latest and greatest news. You create a folder for each story you work on. The folder holds the Word document that contains the basic story, numerous picture files, and an Excel spreadsheet that details location, contacts, expenses, and publications that have been offered the story. If you recast a story for additional markets, that story appears in a subfolder. All of the folders are grouped by year.

The folder arrangement works great, and it simplifies data backup, but the problem is that your laptop appears to be running out of disk space. It might be possible to archive story folders from previous years, but the problem with that idea is that you often like to use pictures that were not published in the earlier stories in later articles. Therefore, a simple archive by date might not be the most effective solution.

You decide to write a Windows PowerShell script that reports folder size so that you can make a more informed decision to reclaim the most disk space and yet maintain access to the most useful content from previous years. An appropriate output is shown in the image that follows.

Image of command output

Design points

  • You do not need to check for admin rights in this script. Assume you have permissions to access the folders you want to check.
  • You should return a custom object to permit further analysis and sorting.
  • You should display folder size in the most appropriate unit of measurement. Therefore, do not report 1073741824 bytes when you could just as easily report 1 Gigabyte.
  • You should report sizes accurate to two decimal places. Do not report 1.23456794 bytes; instead, round it to 1.23 bytes.
  • You should accept a command-line parameter that permits you to supply a specific folder. You should recurse by default.
  • You should consider adding comment-based Help.
  • You should consider creating reusable functions as part of this script.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy 

Comments (19)

  1. mredwilson says:

    @K_Schulte You only need to report the size of each sub folder, not the size of the files in each sub folder.

  2. K_Schulte says:

    Hi Ed,

    after a second thought .. one (more serious) question:

    Should we report the size of a folder as the size of all files in the folder excluding the sizes of each subfolder or including the sizes of each subfolder … or both 🙂 ?


  3. K_Schulte says:

    Root (1MB) +– Sub1 (2MB) +– Sub11 (3MB) +– Sub111 (4MB)

              |              |               +– Sub112 (5MB)

              |              +– Sub12 (6MB)


              +– Sub2 (7MB) +– Sub21 (8MB) — Sub212 (9MB)

                             +– Sub22 (10MB)

    1. Sum up all files in current directory    

    Root   – 1MB

    Sub1   – 2MB

    Sub2   – 7MB

    Sub11  – 3MB

    Sub12  – 6MB

    Sub21  – 8MB

    Sub22  – 10MB

    Sub111 – 4MB

    Sub112 – 5MB

    Sub212 – 9MB

    2. Sum up all files in current folder AND all files in all subfolders

    Root   – 45MB

    Sub1   – 20MB

    Sub2   – 34MB

    Sub11  – 12MB

    Sub12  – 6MB

    Sub21  – 17MB

    Sub22  – 10MB

    Sub111 – 4MB

    Sub112 – 5MB

    Sub212 – 9MB

    3. Sum up all files in all subfolders without files in current folder

    Root   – 44MB

    Sub1   – 18MB

    Sub2   – 27MB

    Sub11  – 9MB

    Sub12  – 0MB

    Sub21  – 9MB

    Sub22  – 0MB

    Sub111 – 0MB

    Sub112 – 0MB

    Sub212 – 0MB

    Hie Ed, what is it, what you wanted us to do?


  4. mredwilson says:

    @Jaap Brasser Your script should report the size of each folder. If you have a sub folder, you should report all the files and folders present in that sub folder as the sub folder size. You then roll up all your sizes into the top folder size. Your goal is to be able to quickly look at a report and tell exactly which folder or sub folder is consuming the most space. Look at the image, it provides an acceptable output.

  5. K_Schulte says:

    Hi Ed,

    this looks like … an easy one!

    In fact I would probably suggest to buy a bigger hard disk at first sight 🙂

    But, as the (ugly) details are often burried behind a short sentence … let's see, what we'll end up with after a couple of overthoughts 🙂


  6. mredwilson says:

    @Ryan Ries I am glad you had fun writing the solution to this event.

  7. mredwilson says:

    @K_Schulte I think scenario 3. Sum the files in each sub folder, but do not include the size of the files in the current folder. Those files are reported separately.

  8. mredwilson says:

    PoshCode is back up and running. Thank you everyone for your understanding. My apologies for any inconvenience

  9. Jaap Brasser says:


    @K_Schulte I think scenario 3. Sum the files in each sub folder, but do not include the size of the files in the current folder. Those files are reported separately.'

    This line threw me off and seems to contradict what is displayed in the image. Would you care to clarify?

  10. mredwilson says:

    @Werner you are right 🙂 It is in the picture.

  11. K_Schulte says:

    Hi Ed,

    now … I don't understand what you said:

    "You only need to report the size of each sub folder, not the size of the files in each sub folder."

    The size of a folder is the size of all files in the folder + ( maybe: the size of each subfolder ) recursively defined.

    I would like to know, which definition holds?

    A folder size can'Ät be measured in sizes of all fsubfolders, if the file sizes in each folder don't count!

    This will always result in NULL!


  12. mredwilson says:

    @K_Schulte this event is not overly tricky, but I did not think it was really simple either. There are a lot of design points for this scenario.

  13. mredwilson says:

    @Ryan yes, it seems that some people are still having problems uploading their scripts. I will post back when upload is back on line.

  14. mredwilson says:

    @scott heath yes.

  15. kassman says:

    @K_Schulte, in my perception the answer is given in the picture. Good luck!

  16. Sergey says:

    I can’t publish my script for this challenge on the site –
    https://2012sg.poshcode.org/. I  click on "Save" and browser redirected to the main page. For log on i use -Google id.

  17. This one was reasonably tough for me.  I had a lot of fun writing it.

  18. But I'm having the same problem as Sergey a couple posts up.

  19. Scott Heath says:

    Do you want us to report folders with a 0 byte size?

Skip to main content