2009-06-27

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.

   1: #First we set the path
   2: $path = Read-Host "Which folder would you like to scan?"
   3:  
   4: #assign what we want to get to a variable
   5: $myfolders = get-childitem $path -force -recurse | where-object{$_.PSIsContainer}
   6:  
   7: #initiate an empty variable
   8: $mycol = @()
   9:  
  10: #now we loop through all the folders 
  11:  
  12: foreach($folder in $myfolders)
  13: {
  14:    Write-Host "Processing Folder $folder"
  15:    #Create and initialize a new variable with two fields Name, SizeMB
  16:    $myObj = "" | Select Name,SizeMB
  17:    #here we measure the actual size of the folder
  18:    [int]$DirSize = "{0:n2}" -f (((Get-Childitem $folder.FullName -recurse -force | ` 
  19:         measure-object -sum Length).Sum)/1mb)
  20:    #add the results to the variable
  21:    $myobj.Name = $folder.Name
  22:    $myobj.SizeMB = $DirSize
  23:    #add these to the mycol variable
  24:    $mycol += $myobj
  25: }
  26:  
  27: #present the output and sort by size
  28: $mycol | sort-object SizeMB -Desc | format-table -auto

Almost at the end!!