find size of a directory

Fun with Powershell - Calculate the size of a given folder

This is a pretty awesome script, what it does is that it asks the user for a folder path they want to calculate the size for and once they key that in, it goes and does what it needs to.(Calculate the size!!)

I had seen  this concept on a MSDN page, however I have extended that base concept into something more interactive. 

Have fun and remember be careful!

$startFolder = Read-Host "Enter the folder path you want to calculate"

write-host "`n"
Write-Host "Calculating the size of the folder $startFolder" -ForegroundColor Green
write-host "`n"
Write-Host "Size of $startFolder" -ForegroundColor Green
write-host "==============="
write-host "`n"
if (-not(Test-Path $startFolder)){
Write-Host "The folder path you have entered is invalid!" -ForegroundColor Red
$collection = (Get-ChildItem $startFolder -Recurse| Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($collection.sum / 1MB) + " MB"
write-host "`n"
Write-Host "Size of all sub-folders under $startFolder" -ForegroundColor Green
write-host "====================================="
$collection = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $collection)
        $subFolderItems = Get-ChildItem $i.FullName | Measure-Object -property length -sum
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
write-host "`n"