title: PowerShell: Incremental Counter in Foreach Loop
body: <p>I have a foreach loop that iterates over an Array and calls a function which also has another foreach loop inside with an incremental counter, however it doesn't seem to be working as expected?</p>
  <p>Array contents:</p>
  <pre><code>| Username | Username2 |
  |----------|-----------|
  | p1       | p2        |
  | p3       | p4        |
  </code></pre>
  <p>Code:</p>
  <pre><code>function insertIntoLunchJobs($arrayOfRows) {
      $counter = 1
  
      foreach ($i in $arrayOfRows) {
          $i
          $counter++
          $counter
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>
  | Username | Username2 |
  |----------|-----------|
  | p1       | p2        |
  | 2        |           |
  | p3       | p4        |
  | 2        |           |
  </code></pre>
  <p>Desired result:</p>
  <pre><code>
  | Username | Username2 |
  |----------|-----------|
  | p1       | p2        |
  | 2        |           |
  | p3       | p4        |
  | 3        |           |
  </code></pre>
  <p>Any ideas?</p>
  <p>TIA</p>