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)