remove specific contact list under outlook

Hi Experts,

I have a list of users that need to be removed from users contact list.   I want to do that using script through GP.

I think best approach is to go through existing user's CSV file and remove it if username available under txt file.

Who is Participating?
andrewjamesbConnect With a Mentor Commented:
Hi uknet

Try this script

$olSession = (New-Object -ComObject Outlook.Application).Session
$olSession.Logon('Outlook') #Outlook is the profile name
$contactsFolder = 10
$tempFolderName = 'temp_folder_' + (get-date -Format ddmmyyyhhmmss)
$myContacts = $olSession.GetDefaultFolder($contactsFolder).Items
$tempFolder = $olSession.GetDefaultFolder($contactsFolder).Folders.Add($tempFolderName)

Write-Host "..getting unique items"
$uniqueContacts = $myContacts | Sort FullName -Unique

#move contacts to temp contacts folder
foreach ($Contact in $uniqueContacts) {
$Contact.Move($tempFolder) | foreach-object {Write-Progress "Backup unique items to temp folder..." $_.FullName; $_.FullName} | Out-Null

#read default contacts again and dump to csv
Write-Host "..export duplicates to csv"
$duplicates = $olSession.GetDefaultFolder($contactsFolder).Items
$duplicates | Export-Csv duplicates.csv

#delete all contacts left in default contacts folder
Foreach ($duplicate in $duplicates){
$duplicate.Delete() | foreach-object {Write-Progress "Deleting duplicate..." $_.FullName; $_.FullName} | Out-Null

Hope this help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.