Deleting or purging a specific contacts folder for all existing users

Hi,

I need to find a way to delete or empty the "Lync Contacts" folder (Subfolder in Contacts) for all existing Exchange users. I've tried narrowing it down by using kind:contacts parameter for the Search-Mailbox command, but that lists all contacts and I can't narrow it down further to that particular folder.

All contacts in the "Lync Contacts" folder have entries like "8/19/2013 This contact was added from Microsoft Lync 2013 (15.0.4517.1004) " in their "Notes" field. I tried to use this to my advantage and phrased my search like this:

Search-Mailbox -Identity "User Name" -SearchQuery all:"from Microsoft Lync 2013"

However, this also returns a few random emails, which don't have that specific string of text, but do mention Lync in their body.

I haven't yet found a way to combine the kind:contacts and the all:"from Microsoft Lync 2013" into one search. Trying to use AND operator or pipe didn't yield any success.

If you know how to combine these two searches or how to delete that folder using any other methods, please let me know.


  • Edited by Sooshi Wednesday, August 21, 2013 4:32 PM
August 21st, 2013 4:31pm

Hello,

After I test it, we only run the Search-Mailbox -Identity "User name" -SearchQuery 'kind:contacts' -DeleteContent cmdlet to delete all contacts. There is no way to combine the kind:contacts and the all:"from Microsoft Lync 2013".

Here is the article for your reference.

Advanced Query Syntax

http://msdn.microsoft.com/en-us/library/aa965711(v=vs.85).aspx

If you have any feedback on our support, please click here

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2013 4:07am

Thank you for your reply Cara! Unfortunately, your answer doesn't provide a solution to my problem. 

Other thoughts:

How can I search for the string "from Microsoft Lync 2013" exclusively, without getting other emails that mention "Lync" in mixed into the search results?

Are there any other commands or scripts besides Search-Mailbox that I can use to delete that folder?


  • Edited by Sooshi Thursday, August 22, 2013 4:32 AM
August 22nd, 2013 4:31am

I found that my syntax was incorrect and that was the reason for rogue emails in my search results.

Instead of Search-Mailbox -Identity "User Name" -SearchQuery all:"from Microsoft Lync 2013" I used Search-Mailbox -Identity "User Name" -SearchQuery 'all:"from Microsoft Lync 2013"' and that returned result which contain "from Microsoft Lync 2013" exclusively.

I will use this workaround to delete all Lync Contacts.

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2013 8:15pm

Hello,

I research it again.

There seems no better way.

Thank you for your sharing.

If you have any feedback on our support, please click here

August 23rd, 2013 1:42am

Hello,

I research it again.

There seems no better way.



I am novice in Exchange but I assume the below code does as expected

#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 
}
Free Windows Admin Tool Kit Click here and download it now
April 3rd, 2015 3:27am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics