title: Powershell ForEach replace (Bulk change primary SMTP in AD)
body: <p>I need switch the primary SMTP address in AD in bulk from users of an certain OU.</p>
  <p>The challenge;</p>
  <pre><code>User1  
  smtp:first.last@domain1.com  
  smtp:flast@domain1.com  
  SMTP:first.last@domain2.net  
  smtp:flast@domain2.net  
  </code></pre>
  <p>I need to make the first.last@domain1 the primary SMTP.</p>
  <p>So far I have come to this;</p>
  <pre><code>$proxies = $null
  Get-ADUser -Filter * -SearchBase &quot;OU=users_test,OU=Test,DC=test,DC=local&quot; -Properties name,mail,ProxyAddresses |
      Foreach {  
          $proxies = $_.ProxyAddresses | 
              ForEach-Object{
                  $a = $_ -replace 'SMTP','smtp'
                  if($a -match 'domain1.com'){
                      $a -replace 'smtp','SMTP'
                      Write-Host $a
                  }else{
                      $a
                  }
              }
          $_.ProxyAddresses = $proxies
          #Set-ADUser -instance $_
          Write-host $proxies
      }
  </code></pre>
  <p>The problem:</p>
  <p>When I run the above script it obviously make both aliases with domain1.com the primary by replacing the smtp with SMTP on all that it finds matching the domain1.com.</p>
  <p>Question: How can I make so it replaces only one?</p>
  <p>I hope I explain myself good enough. Thank you in advance for any coming help </p>