title: How to total Kbytes of several files
body: <p>I'm looping through a directory full of files and need to get the grand total of all the files in a directory.  However I'm getting a conversion problem due to the way I fetch/calculate the kilobytes.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>$destinationDir = "\\server\folder"
  $filename = "FILE_1234567.xls"
  $filesize = 0
  $totalfilesize = 0
  
  $filesize = Get-ChildItem ($destinationDir + "\" + $filename) | Select-Object @{Name="Kbytes";Expression={"{0:N0}" -f ($_.Length / 1Kb)}}
  $totalfilesize += [int]($filesize)
  
  Write-Host $filesize
  Write-Host $totalfilesize
  </code></pre>
  
  <p>However when I run this I get the following error message:</p>
  
  <pre><code>Cannot convert the "@{Kbytes=93}" value of type "Selected.System.IO.FileInfo" to type "System.Int32".
  At C:\Sample.ps1:7 char:18
  + $totalfilesize += &lt;&lt;&lt;&lt;  $filesize
  + CategoryInfo          : NotSpecified: (:) [], RuntimeException
  + FullyQualifiedErrorId : RuntimeException
  </code></pre>
  
  <p>I'm pretty new to Powershell but it seems like I cannot cast <code>$filesize</code> to an integer for totaling.  Can anyone help with where I've gone wrong?</p>