title: How can I repeat an iteration in a PowerShell foreach loop?
body: <p>I frequently have to loop through sets of data (SQL results, lists of computers, etc.) in PowerShell, performing the same operation (e.g function X) on each time. I use <code>foreach</code> loops almost exclusively as they are simple, effective and easily understood by others who may need to follow my code.</p>
  
  <p>I would like to make some of my code more robust, in the sense of retrying operations that fail (up to Y times). There's more than one way to achieve this, for example <em>within</em> the foreach loop, wrapping function X in a <code>do{} while()</code> loop. In this example, assume that function X only returns non-null results when it is "successful":</p>
  
  <pre><code>foreach($dataitem in $dataset){
    $Result = $null
    $Attempts = 0
    do{
      $Attempts++
      $Result = &lt;call function X on $dataitem&gt;
    } while(($Attempts -lt 3) -and (-not $Result))
  }
  </code></pre>
  
  <p>I was wondering whether there was any way to flatten this logic a bit, i.e. a more advanced way of using foreach loops, so I can do away with the <code>do{} while()</code> loop.</p>
  
  <p>I have encountered the opposite of what I want, namely using <code>$foreach.MoveNext()</code> to skip forwards in the loop, but haven't found anything that (dangerously?) would keep foreach processing the same item.</p>
  
  <p>Essentially: <strong>Can a foreach loop be made to re-do an iteration?</strong>.</p>