DEV Community

Cover image for Find the Size of Subfolders Using a One-Liner PowerShell Command
Tahsin Abrar
Tahsin Abrar

Posted on • Edited on

Find the Size of Subfolders Using a One-Liner PowerShell Command

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) }"
Enter fullscreen mode Exit fullscreen mode

โœ… 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฌ 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)
}
Enter fullscreen mode Exit fullscreen mode

Then run it with:

powershell -ExecutionPolicy Bypass -File .\FolderSizeViewer.ps1
Enter fullscreen mode Exit fullscreen mode

๐Ÿงต 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

Top comments (0)