Share via


O365 : How to Delete Contacts using PowerShell and CSV File

The steps below show how we can delete multiple contacts by using a PowerShell script and a CSV file.

 

Step 1. Create the CSV File

On the first step we need to create a csv file with the column "UserPrincipalName

UserPrincipalName

user1@contoso.com

user2@contoso.com

user3@contoso.com

user4@contoso.com

user5@contoso.com

user6@contoso.com

user7@contoso.com

user8@contoso.com

user9@contoso.com

user10@contoso.com 

Step 2. Run the PowerShell script

The second step is to run the below PowerShell script.

 Note: Change the path and name of your csv file. 
Import-Module ExchangeOnlineManagement
 
Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $truE
 
$users = Import-Csv C:\CSV \deletecontact.csv
 
foreach ($user in $users)
 
{
 
$ExternalEmailAddress=$user.ExternalEmailAddress
 
Remove-MailContact $ExternalEmailAddress
 
}
 
Write-Host "DONE RUNNING SCRIPT, CHECK FOR ERRORS"
 
Read-Host -Prompt "Press Enter to exit"