Powershell Scripting Games - Day 8

Here is my solution for Event 8. Here we had to go through the hard drive and report which folder was using up all the space on the drive.

#First we set the path
$path = Read-Host "Which folder would you like to scan?"

#assign what we want to get to a variable
$myfolders = get-childitem $path -force -recurse | where-object{$_.PSIsContainer}

#initiate an empty variable
$mycol = @()

#now we loop through all the folders 

foreach($folder in $myfolders)
   Write-Host "Processing Folder $folder"
   #Create and initialize a new variable with two fields Name, SizeMB
   $myObj = "" | Select Name,SizeMB
   #here we measure the actual size of the folder
   [int]$DirSize = "{0:n2}" -f (((Get-Childitem $folder.FullName -recurse -force | ` 
        measure-object -sum Length).Sum)/1mb)
   #add the results to the variable
   $myobj.Name = $folder.Name
   $myobj.SizeMB = $DirSize
   #add these to the mycol variable
   $mycol += $myobj

#present the output and sort by size
$mycol | sort-object SizeMB -Desc | format-table -auto

Almost at the end!!