Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Top Level Folder Usage

Posted on July 4, 2011July 4, 2011

This is too long to tweet, even written as a one liner. But this will search a folder for top level subfolders and return the file usage for each.

[cc lang="PowerShell"]
$folder="S:\"
dir $folder | where {$_.psIscontainer} | foreach {
$stat= dir $_.fullname -recurse | Measure-Object -property length -Sum
New-Object PSObject -property @{Folder=$_.FullName;Files=$stat.count;Size=$stat.Sum;}}
[/cc]

Again, this would be handy to turn into a function or scriptblock. This is far from perfect. For example, it doesn't like empty folders. But this should be a good starting point for your own script or function.

Share this:

  • Print (Opens in new window) Print

Like this:

Like Loading…

Related

5 thoughts on “Get Top Level Folder Usage”

  1. Vincent says:
    July 5, 2011 at 3:51 am

    Awesome. Thank you.

    I don’t like empty folders either, so that’s ok 🙂

  2. Rudy says:
    December 12, 2011 at 10:58 am

    Thank you very much I have been looking for something like this for the past 2 days. I just didn’t know the correct verbage to use while I was searching google. As I am still really new to powershell I was wondering is there a way to export this to a CSV file?
    I am shooting in the dark here would it be something similar to:
    $stat | export-csv -path test.csv

    Thanks for the one liner
    Rudy

    1. Jeffery Hicks says:
      December 12, 2011 at 11:16 am

      That is exactly how you would do it.

      1. Rudy says:
        December 12, 2011 at 11:24 am

        Cool thank you very much! Would it be easy to change it to display in MB instead of KB?

      2. Jeffery Hicks says:
        December 12, 2011 at 12:56 pm

        Of course. Just change the division

        SizeMB=$stats.sum/1MB

Comments are closed.

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2026 The Lonely Administrator | Powered by SuperbThemes!
%d