title: Get-ADUser Check for conflicting proxyAddresses
body: <p>Currently I have a script that creates user accounts. </p>
  
  <p><strong>Note:</strong> Not all users have the same UPN (UserPrincipalName)</p>
  
  <p>User accounts are in the following format: <code>&lt;firstinit&gt;&lt;lastname&gt;</code>. </p>
  
  <p>If this conflicts, the format will be changed to: <code>&lt;firstinit&gt;&lt;middleinit&gt;&lt;lastname&gt;</code></p>
  
  <p>Recently I have ran into an issue where the user's <code>proxyAddress</code> is conflicting with existing users. This is a problem because AD will not catch this. </p>
  
  <p><strong>Issue:</strong></p>
  
  <p>Checking every <code>AD-User</code>'s proxy address is very time consuming if not included in the filter. However, when including <code>proxyAddresses</code> in the filter the results are inconsistent. I am assuming this is because the <code>proxyAddresses</code> attribute is an array. </p>
  
  <p><strong>Inconsistent</strong>:</p>
  
  <pre><code>Import-Module ActiveDirectory
  $FirstLast = "jrider@ChuckNorrisKills.com"
  
  $conflictCheck = Get-ADUser -Properties mail, proxyAddresses -Filter "mail -eq '$FirstLast' -or UserPrincipalName -eq '$FirstLast' -or proxyAddresses -eq `"smtp:'$FirstLast'`"" | measure
  if($conflictCheck.Count -gt 0)
  {
      Write-Host "New user conflicts with existing user" -ForegroundColor Red 
  }
  </code></pre>
  
  <p>I have come up with a solution that will resolve me issue. Unfortunately this is very slow (expected):</p>
  
  <pre><code>Import-Module ActiveDirectory
  function Test-NewADUser
  {    
      Param(
          [Parameter(Mandatory=$true)][string]$firstname, 
          [Parameter(Mandatory=$true)][string]$lastname,         
          [Parameter(Mandatory=$false)][string]$middle        
      )    
      [bool]$proxExsists = $false
  
      $domain = '@chuckNorrisKills.com'    
      $FirstLast = $firstname.Substring(0,1)+$lastname+$domain
      Get-ADUser -Filter * -Properties proxyAddresses | foreach {
              #xpand the proxy address and iterate through it
              foreach($address in $_.proxyAddresses)
              {
                  #As you can see this goes through every user
                  Write-Host "Address: " $address -ForegroundColor Yellow                
                  if($address -eq "smtp:$FirstLast")
                  {
                      Write-Host "Found Conflict" -ForegroundColor Red
                      $proxExsists = $true
                  }
              }            
          }   
  }
  
  Test-NewADUser -firstname jack -lastname Rider
  </code></pre>
  
  <p><strong>Question(s):</strong></p>
  
  <ol>
  <li>Is there a way to expand <code>proxyAddresses</code> and check for conflicts in the <code>-Filter</code>? </li>
  <li>If not, should I bother with Jobs, or an alternate way of checking for conflicts?</li>
  </ol>
  
  <p>Thank you in advance for any help</p>