title: PowerShell find and replace using an array of strings
body: <p>I'm wanting to comb through an LDIF file for 28 strings to replace. I have two arrays, one for the find, and another for the replace items.</p>
  <p>The replace is working, but is then overwritten on the next iteration. I don't know how to keep the existing file intact and only change the row with the string needing replaced.</p>
  <pre><code>
  $find = @(  &quot;Code=2092&quot;,
              &quot;Code=3257&quot;,
              &quot;Code=3044&quot;,
              &quot;Code=3001&quot;
  )
  
  $replace = @(
                  &quot;|(Code=2092)(Code=9191)(Code=9192)(Code=9193)(Code=9194)&quot;,
                  &quot;|(Code=3257)(Code=5581)(Code=5582)(Code=5583)(Code=5584)&quot;,
                  &quot;|(Code=3044)(Code=5591)(Code=5592)(Code=5593)(Code=5594)&quot;,
                  &quot;|(Code=3001)(Code=5601)(Code=5602)(Code=5603)(Code=5604)&quot;
  )
  
  $filePath = 'C:\Users\tim\Codes\export.ldf'
  $tempFilePath = 'C:\Users\tim\Codes\temp.ldf'
  
  $i = $find.Length
  Write-Output $i
  
  for ($n = 0; $n -lt $i + 1; $n++)
  { 
      (Get-Content -Path $filePath) | ForEach-Object {$_ -replace $find[$n], $replace[$n]} | Add-Content -Path $tempFilePath
      Write-Output $n
      Write-Output $i
  }
  Remove-Item -Path $filePath
  Write-Output &quot;Finished with Array&quot;
  Move-Item -Path $tempFilePath -Destination $filePath
  
  </code></pre>
  <p><strong>A Sample input is:</strong></p>
  <pre><code>#This LDIF file
  version: 1
  
  
  dn:cn=DG-C-10292-718200,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=718200)(Code=10292)(!(State=Stale)))
  
  dn:cn=DG-C-10325-764792,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Code=10325)(Reference=764792)(!(State=Stale)))
  
  dn:cn=DG-C-10400-519400,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=519400)(Code=10400)(!(State=Stale)))
  
  dn:cn=DG-C-10454-745581,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=745581)(Code=10454)(!(State=Stale)))
  
  dn:cn=DG-C-10547-115900,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=115900)(Code=10547)(!(State=Stale)))
  
  dn:cn=DG-C-10548-719708,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=719708)(Code=10548)(!(State=Stale)))
  
  dn:cn=DG-C-10548-719715,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=719715)(Code=10548)(!(State=Stale)))
  
  dn:cn=DG-C-10550-719700,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=719700)(Code=10550)(!(State=Stale)))
  
  dn:cn=DG-C-10572-719714,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=719714)(Code=10572)(!(State=Stale)))
  
  dn:cn=DG-C-10578-719700,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=719700)(Code=10578)(!(State=Stale)))
  
  dn:cn=DG-C-10580-718600,OU=R,DC=ORG
  changetype:add
  QueryURL:ldap:///OU=Codes,DC=Org??one?(&amp;(Reference=718600)(Code=10580)(!(State=Stale)))
  </code></pre>