PowerShell Script: Purge and Delete Lync Contacts in Outlook


PowerShell Script: Purge and Delete Lync Contacts in Outlook


Summary

One of our client requested to remove LYNC Contact folders from Outlook. We can't remove the folder directly since it's a read only folder. Why to delete this folder? Just to solve duplicate contact issues!

Background

There is no straight forward approach to do this. So decided to take full owner ship of users mail box and perform delete operations. Well! that's not a good approach. Exchange online will Auto Map the mail box if we have full permission. Yeah! we can set it to false and perform this. Accidental execution of code may end up in collision and do remember Auto Mapping needs at least 4 hours to update.

Solution

Why don't we impersonate and perform this using PowerShell and EWS?. The below code will purge and delete the Lync Contacts

#Script to delete LYNC Contacts Folder

#Bulk Import Mailnox ID from CSV

$MailboxNames =  "Mailbox1" , "Mailbox2"

#Any Exchange Admin ID with appropriate permissions
$AdminID = "AdminID"

#Fetch password as secure string
$AdminPwd = Read-Host "Enter Password" -AsSecureString

#Load the Exchange Web Service DLL
$dllpath = "C:\Microsoft.Exchange.WebServices.dll"
[Reflection.Assembly]::LoadFile($dllpath)

#Create a Exchange Web Service
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)

#Credentials to impersonate the mail box
$Service.Credentials = New-Object System.Net.NetworkCredential($AdminID , $AdminPwd)
foreach($MailboxName in $MailboxNames)
{
#Impersonate using Exchange WebService Class
$Service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
$Service.AutodiscoverUrl($MailboxName,{$true})

#Assing EWS URL
$service.Url = 'https://outlook.office365.com/EWS/Exchange.asmx'
Write-Host "Processing Mailbox: $MailboxName" -ForegroundColor Green

#Fetch Root Folder ID
$RootFolderID = New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $MailboxName)
$RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$RootFolderID)

#Create a Folder View
$FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
$FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep

#Retrieve Folders from view
$response = $RootFolder.FindFolders($FolderView)

#Query Folder which has display name like Lync Contacts
$Folder =  $response | ? {$_.FolderClass -eq 'IPF.Contact.MOC.QuickContacts'}
$Folder | Select DisplayName , TotalCount

#Purge the items
$Folder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete, $false)
$Folder.Load()


#Perform Delete
$Folder.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete)
Write-Host "Removed Lync Contacts Folder on: $MailboxName mailbox" -ForegroundColor Green
#$Folder | ? {$_.TotalCount -gt 0 } | Select DisplayName , TotalCount
}

Screenshots

So we can see 832 entries in the Lync Contact Folder. After executing the above PowerShell Code we can't see the LYNC Contact folder for a while. Disable Cached Mode and check it shows 0 entries. Or we can see absence of LYNC Contact folder in OWA.