title: How to automate removing Exchange Contacts in Office365
body: <p>I am writing a powershell script that:</p>
  
  <ol>
  <li>Compares two CSV files</li>
  <li>Output files for: Changes, added, removed contacts</li>
  <li>Update and add contacts</li>
  <li>Remove contacts</li>
  </ol>
  
  <p>The problem is when I try and removed contacts. Which is done by:</p>
  
  <pre><code>  #Check for Removed Contacts
     foreach($row in $File1_Data )
    {
      $data_found=0
       foreach($id in $emails_id)
        {
         if ($row.ExternalEmailAddress -eq $id)
           {
            $data_found=1
  
           }
  
        } 
  
     if($data_found -eq 0 ) #Email Not Found
        { $row|Select-Object -Property ExternalEmailAddress|Export-Csv -Path $Removed_Contact -Append -NoTypeInformation}
  
    }
  </code></pre>
  
  <p>Now I have a file with only the email addresses. The error comes when I try and run the command connected on the exchange server.</p>
  
  <pre><code>$RemoveContacts = Import-CSV ".\Removed Contacts_$((Get-Date).ToString('MMddyyyy')).csv"
  $RemoveContacts | ForEach { Remove-MailContact -identity $_ -confirm:$false}
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <blockquote>
    <p>Cannot process argument transformation on parameter 'Identity'. Cannot convert the
    "@{ExternalEmailAddress=testuser@testcompany.com}" value of type "Deserialized.System.Management.Automation.PSCustomObject"
    to type "Microsoft.Exchange.Configuration.Tasks.MailContactIdParameter".
        + CategoryInfo          : InvalidData: (:) [Remove-MailContact], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Remove-MailContact
        + PSComputerName        : outlook.office365.com</p>
  </blockquote>
  
  <p>$File_Data structure is <a href="https://docs.microsoft.com/en-us/office365/securitycompliance/bulk-import-external-contacts" rel="nofollow noreferrer">in the format Microsoft requires</a>.
  and
  $emails_id is the function that compares the two csv files. But that's not where the script breaks, that's just how i create the file. 
  What am I missing?</p>