title: Updating user ProxyAddresses
body: <p>I try to update AD user account ProxyAddresses property. I've read many topics about this and applied one of the suggested approach (<a href="https://social.technet.microsoft.com/Forums/en-US/e3970a4a-287c-4029-af89-5904a55b3a61/change-primary-smtp-in-proxyaddresses-ad-attribute?forum=winserverpowershell" rel="nofollow noreferrer">this one</a>) but it doesn't work for me. Why?
  I use a following bit of code (it's a part of script updating more users data):</p>
  
  <pre><code>$ADUser = SearchADUser -Kogo "sAMAccountName -eq '$($WzorUser.sAMAccountName)'"
  ...
  1.$ProxyOK = $false
  2.$Proxies = $ADUser.ProxyAddresses
  3.$Proxies | ForEach-Object {
  4.  $_ = $_ -replace 'SMTP', 'smtp'
  5.  if ($_ -match $NoweMail) {
  6.      $_ = $_ -replace 'smtp', 'SMTP'
  7.      $ProxyOK = $true
  8.  }
  9.}
  10.if (!($ProxyOK)) { $Proxies += ("SMTP:$($NoweMail)") }
  ...
  if (!([string]::IsNullOrEmpty($Proxies))) {
      $AttrToReplace.Add("ProxyAddresses", $Proxies)
  Set-ADUser -Identity $ADUser.sAMAccountName -Server $ADDC @Attr #-PassThru -WhatIf
  </code></pre>
  
  <p>When looping on Proxies, its elements are properly processed: letters are lowercased for all and uppercased if the new mail was already present.
  But Proxies is not changed. Do each element needs to be saved in some way or replaced in the object?</p>
  
  <p><strong>Update 2020-04-02</strong></p>
  
  <p>Because supporter effort (@Theo once again thanks for Your assist) focus on replacing method I try to more detailed explain my problem.</p>
  
  <p>Target user proxyAddresses values (it's a rare case when women come back to her maiden name alredy recorded in AD):
  <br><strong>SMTP</strong>:anna.nowak22@lp.pl
  <br><strong>smtp</strong>:a.b@moc.com</p>
  
  <p>Initial  values (line 2):</p>
  
  <pre><code>[DBG]: PS X:\&gt;&gt; $Proxies
  smtp:anna.nowak22@lp.pl
  SMTP:a.b@moc.com
  </code></pre>
  
  <p>While looping on every proxy element (line 9):</p>
  
  <pre><code>[DBG]: PS X:\&gt;&gt; $_
  SMTP:anna.nowak22@lp.pl
  [DBG]: PS X:\&gt;&gt; $Proxies
  smtp:anna.nowak22@lp.pl
  SMTP:a.b@moc.com
  
  [DBG]: PS X:\&gt;&gt; $_
  smtp:a.b@moc.com
  [DBG]: PS X:\&gt;&gt; $Proxies
  smtp:anna.nowak22@lp.pl
  SMTP:a.b@moc.com
  </code></pre>
  
  <p>As can be seen $Proxies doesn't reflect changes.</p>
  
  <p>If there was only one ProxyAddresses value not equal with new mail, new mail was added as SMTP to the existed one which is leaved also as SMTP (two primary ProxyAddresses).</p>
  
  <p>I tried to create a new variable and assign to it each value respectively but I don't know how to handle it.</p>
  
  <pre><code>$newProxies = $null
  $Proxies | ForEach-Object {
      $_ = $_ -creplace 'SMTP', 'smtp'
      if ($_ -match $NoweMail) {
          $_ = $_ -creplace 'smtp', 'SMTP'
          $ProxyOK = $true
      }
        $newProxies.add($_)
  }
  </code></pre>
  
  <p>Above generates an error</p>
  
  <blockquote>
    <p>You cannot call a method on a null-valued expression</p>
  </blockquote>
  
  <p><code>$newProxies += $_</code> creates one string <em>SMTP:anna.nowak22@lp.pl
  smtp:a.b@moc.com</em> which is added as single ProxyAddress.</p>
  
  <p>As I noted $Proxies is a special AD object and I don't know how to create an object of such type and how to add new elements to it.</p>