Sunday, October 2, 2011

Display Top Level Folder/File Sizes

It’s been a long, long time since my last post. Last week, I was having a problem with my office hard disk. It doesn’t have much disk space anymore and it’s performing very, very slow. I thought it would be nice to have a powershell script which displays the top level folder/file sizes in a given path. It’s also cool to have the largest folder/file displayed on top.

I’m a huge fan of one-liners. Hence, here’s my solution in one powershell line.

  1. ls | select Name, @{Name="Type";Expression={if($_.psIsContainer){"Directory"}else{"Folder"}}}, @{Name="Size(MB)";Expression={[Math]::Round($(ls $_.FullName -recurse| measure Length -sum).Sum/1MB, 2)}}| sort -property "Size(MB)" -desc 

Note: This script also indicates whether it’s a folder or file and rounds off the size to 2 decimal points in MB.