title: Replace the first occurence of a string in a file
body: <p>In a PowerShell script, to replace the first occurrence of a string in a file I came with the code below, which keeps track in a variable whether the replacement was made.</p>
  
  <p>Is there a more elegant (idiomatic) way of doing this?</p>
  
  <pre><code>$original_file = 'pom.xml'
  $destination_file =  'pom.xml.new'
  
  $done = $false
  (Get-Content $original_file) | Foreach-Object {
      $done
      if ($done) {
          $_
      } else {
          $result = $_ -replace '&lt;version&gt;6.1.26.p1&lt;/version&gt;', '&lt;version&gt;6.1.26.p1-SNAPSHOT&lt;/version&gt;'
          if ($result -ne $_) {
              $done = $true
          }
          $result
      }
  } | Set-Content $destination_file
  </code></pre>