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
- Open Command Prompt in the folder you want to analyze.
- Paste and run the command.
- 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
 
 
              
 
    
Top comments (0)