title: Using a foreach loop to tally up columns, adding sum at the end
body: <p>I'm trying to take a CSV, loop through it line by line, field by field, grabbing the values of two columns, perform some simple math, then set the results to a column on the end. I think I've got it down for the most part, but I can't get it to export correctly. </p>
  
  <p>I've tried iterating through, changing the <code>$field.Value</code> of the final field and exporting the CSV, but it spits it out wrong. I've also tried outputting each line to a new csv, but can't figure out a way to filter out just the values from the key:value pairs.</p>
  
  <pre><code>Import-Csv $csv -Header "SubmissionID","SubmitterName","JobDate","JobTime","TotalDocs","AddnPages","InputFilename","ActualDocs" |
    foreach { 
      foreach ($field in $_.PSObject.Properties) {
        if ($global:count -eq 4) {
          [int]$global:totaldocs = $field.Value
        }
        if ($global:count -eq 5) {
          [int]$global:addnpages = $field.Value
        }
        if ($global:count -eq 7) {
          [int]$global:actualdocs = $global:totaldocs - $global:addnpages
          $global:count = -1
          $field.Value = $global:actualdocs
          Add-Content $outcsv $_.Value
        }
        $global:count = $global:count + 1
      }
    } | Export-Csv $outcsv -NoTypeInformation
  </code></pre>