title: Calculate before and after folder sizes when zipping
body: <p>I have the code below which works fine for zipping files that meet my criteria and deleting the files, but what I want to add is for it to calculate the folder size before and after and determine the difference.  This sort of works but not very well.  Wondering if there is a better way.  Keep in mind this script typically runs for 3-4 days as it traverses our file system that is over 3TB in size.  So I want it to basically report not just what it cleaned, but how much space it reclaimed.</p>
  
  <pre><code>$Before = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum).Sum
  
  Function LogWrite
  {
  Param ([string]$logstring)
  
  Add-content $Logfile -value $logstring
  }
  
  $PathToZipper="C:\Program Files\7-Zip"
  $UDSFilesFolder="U:\UDS\Test"
  $FileMask="*.sas7bdat"
  $TimeBound="12/31/2015"
  dir $UDSFilesFolder -Recurse -Filter $FileMask|where{$_.LastWriteTime -lt  $TimeBound}|foreach{
  $PathToFile=Split-Path $_.FullName
  $PrevSize=(Get-ChildItem $pwd.path | Measure-Object -property length -sum).Sum
  $ZipProcess=Start-Process "$PathToZipper\7z.exe"  -ArgumentList " a -tzip `"$PathToFile\$($_.BaseName).zip`" `"$($_.FullName)`"" -Wait -PassThru
  if (!$ZipProcess.ExitCode) {
      del $_.FullName
      LogWrite $_.FullName
  }
      $AfterSize=(Get-ChildItem $pwd.path | Measure-Object -property length -sum).Sum
      $Diff = "{0:N2}" -f (($PrevSize-$AfterSize)/1MB)
      LogWrite $PrevSize, $AfterSize, $Diff
  }
  
  $After = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum).Sum
  $Calc = "{0:N2}" -f (($Before-$After)/1MB)
  
  # *********************************************************************************************************** 
  #   Now lets cleanup LogFiles older than 30 days
  # *********************************************************************************************************** 
  
  Get-ChildItem *.log | where {$_.LastWriteTime -le (Get-Date).AddDays(-30)} | remove-item -verbose
  
  
  $smtpServer = "mailserver"
  $smtpFrom = "from"
  $smtpTo = "to"
  $messageSubject = "Sample File Maintenance Script Task Completed (UDS)"
  $messageBody = @"
  This email is to inform you that the regularly scheduled tmp file maintenance script task completed successfully on DTWFP3.  
  
  The script cleared up an extra $Calc MB of disk space.
  
  Current folder size is: $($colItems = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum)
  "{0:N2}" -f ($colItems.sum / 1MB) + " MB")
  "@
  
  
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  $smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
  </code></pre>