by Njål

Outlook 2010 & Powershell: Importing all email addresses in Sent Items to Autocomplete list

If you have reinstalled Outlook or perhaps set up your email account on a new PC – you’ll be pretty annoyed that the AutoComplete list in Outlook is empty.

This list has nothing to do with your contact list – it’s just a list of people you have sent emails to. Until Outlook 2010 this list was handled locally by Outlook – not on the Exchange/email server. The list used to be a relatively simple .nk2 file.

 

In Outlook 2010 this list is now stored in a separate Address Group called Suggested Contacts.  You’ll find this Group under Contacts in Outlook. Like any other Address Group.

 

 

This is actually a lot better than using the nk2 files – which had the habit of corrupting themselves. Another advantage is that the Autocomplete list in Outlook 2010 should work across different pc’s – given you are using Exchange server (or some other ActiveSync server like Kerio etc.)

Nevertheless – here’s a script that loops through all Emails in SentItems and adds Names & Emails of Recipients to the Suggested Contacts group. This will make your autocomplete work like a charm again. The script can relatively easy be modified to scan through your inbox and add all email addresses there as well.

 

# OUTLOOK AUTOCOMPLETE POWERIMPORT
#
# Author: N. Gjermundshaug - Degree Consulting Group AS - www.degree.no
#
# This script requires Outlook to be installed and configured. It scans through the SentItems folder.
# For each sent email, it loops through each Recipient - and adds the recipient to the "Suggested Contacts" Address Group.
# Contacts in the "Suggested Contacts" will be displayed in the autocomplete field - when composing new emails (like nk2).
# Contacts are only added once from the script.

$outlook = new-object -com outlook.application
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$namespace = $outlook.GetNameSpace("MAPI")
$sentItems = $namespace.getDefaultFolder($olFolders::olFolderSentMail)
$alreadyAddedEmails = @() #Empty Array
$counter = 0;
$totalItems = $sentItems.items.count;

Write-Host "Scanning through" $totalItems "emails in SentItems"

$contacts = $outlook.Session.GetDefaultFolder($olFolders::olFolderSuggestedContacts)


##############################################################################################################
# FUNCTION - Adds Name/Email to SuggestedContacts - Unless it has already been added before (by this script).
##############################################################################################################
Function AddToSuggestedContactsIfNotAlreadyAdded ($name, $email)
{

    if(($name -eq "") -or ($email -eq "") -or ($name -eq $null) -or ($email -eq $null)){
        return;
    }

    $name = $name.Replace("'", "").Replace("""", "")


    $contactAlreadyAdded = $false

    foreach ($elem in $global:alreadyAddedEmails) {
        if(($elem.ToLower() -eq $email.ToLower())){
            $contactAlreadyAdded = $true
            Write-Host  ($global:counter)"/"($totalItems)  "SKIPPED " $name.PadRight(25," ") "-" $email
            return;
        }
    }

    if(!$contactAlreadyAdded )    {
        $newcontact = $contacts.Items.Add()
        $newcontact.FullName = $name
        $newcontact.Email1Address = $email
        $newcontact.Save()
        $global:alreadyAddedEmails += $email
        Write-Host ($global:counter)"/"($totalItems)  "ADDED   " $name.PadRight(25," ") "-" $email
    }
}

# Loop through all emails in SentItems
$sentItems.Items | % {

    #Loop through each recipient
    $_.Recipients | %{
        AddToSuggestedContactsIfNotAlreadyAdded $_.Name  $_.Address
    }
    $global:counter = $global:counter + 1
}

Write-Host "Done!"
$outlook.Quit()

 

 

Other keywords: Rebuild Cache Restore Entry Post Migration Auto Complete Repair Batch Bulk Auto Completion PS1 Import