If you're working on Windows and ever wondered which subfolders are taking up the most space, here's a neat PowerShell one-liner that will save your day.

🧠 The PowerShell One-Liner

powershell -command "Get-ChildItem -Directory | Sort-Object { (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum } -Descending | ForEach-Object { $size = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum; \"{0,-30} - {1:N2} MB\" -f $_.Name, ($size / 1MB) }"

✅ What It Does

This command, when run inside any folder using Windows Command Prompt, will:

  • 🔍 Scan all immediate subfolders.
  • 📊 Calculate their total size, including all nested files.
  • 📁 Sort the folders by size, from largest to smallest.
  • 📃 Display the name and size of each folder in a neat, readable format.

🖥 Example Output:

node_modules                 - 450.25 MB
dist                         - 120.53 MB
logs                         - 30.47 MB

🔬 Breakdown of the Command

Command Part What It Does
Get-ChildItem -Directory Lists all subdirectories in the current folder.
Sort-Object { (Get-ChildItem ...).Sum } -Descending Calculates size of each folder recursively and sorts them from largest to smallest.
Measure-Object -Property Length -Sum Sums up the size of all files.
ForEach-Object { ... } Loops through each folder and prints formatted size in MB.

🚀 How to Use It

  1. Open Command Prompt in the folder you want to analyze.
  2. Paste and run the command.
  3. Instantly view the sizes of all subfolders, formatted for readability.

💡 Tip: If you prefer using PowerShell directly, remove the powershell -command prefix.


🛠 Use Cases

  • Quickly free up space by identifying large folders.
  • Analyze project folders like node_modules, vendor, dist, etc.
  • Useful during CI/CD, backup preparation, or cleanup tasks.

📦 Bonus: Create a .ps1 Script

Save this logic into a .ps1 file for quick reuse:

Get-ChildItem -Directory |
Sort-Object { (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum } -Descending |
ForEach-Object {
    $size = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
    "{0,-30} - {1:N2} MB" -f $_.Name, ($size / 1MB)
}

Then run it with:

powershell -ExecutionPolicy Bypass -File .\FolderSizeViewer.ps1

🧵 Final Thoughts

This simple PowerShell trick gives you deep insight into your folder sizes without installing any third-party tools. Perfect for developers, sysadmins, and anyone looking to keep their directories clean and optimized.


🔖 Tags:

#powershell #windows #cli #developer #tips #sysadmin #devops #productivity