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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

[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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

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.

reports

Powered by Buttondown.

Join me on Mastodon

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

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