How To Export Lync Contacts to Excel


Operator, oh could you help me place this call?

See, the number on the matchbook is old and faded…

– Jim Croce, “Operator (That’s Not the Way it Feels)”

 

 

When was the last time you called the telephone operator? Do operators even exist anymore? With your lists of contacts in your cell phone, Outlook, and Lync, how could you ever be without the number you need? And with everything all nicely synched up, you have all the numbers you need regardless of the device or the application you’re using. Right?

 

Okay, maybe not.

 

It’s possible that maybe things aren’t always synched. Because of that you might want to export your contacts from an application so you can either import them somewhere else, save them as a backup, or just have them available in another format. However, you’ll soon discover that Microsoft Lync 2010 doesn’t actually have an Export feature. But that’s okay, we’ve come up with a script to remedy that situation. This script will export your Lync 2010 contacts to a Microsoft Excel spreadsheet.

 

Requirements. Before you get too excited about this, there are a few things you need to know about. First of all, this script runs only on the client machine and applies to the currently logged-in Lync user. Lync must be running and you must be logged in to it for this script to work.

 

Second, you must have the Microsoft Lync 2010 SDK installed on the client machine.

Download the Lync 2010 SDK.

 

And, of course, third, you need Windows PowerShell 2.0. You don’t need the Lync Server Management Shell; this script runs against Lync, not Lync Server.

 

Oh yeah, you also need Microsoft Excel.

 

I’ve overcome the blow, I’ve learned to take it well

I only wish my words could just convince myself…

 

Hey, come on, the requirements aren’t that bad!

 

We’re going to start by showing you a stripped-down version of the script. In this version we retrieve all the contact information and display it to the screen. After walking you through how to retrieve all the information we’ll show you the full script that exports everything to Excel.

 

Ready? Okay, here we go:

 

$assemblyPath = “C:Program Files (x86)Microsoft LyncSDKAssembliesDesktopMicrosoft.Lync.Model.DLL”

Import-Module $assemblyPath

 

$DisplayName = 10

$PrimaryEmailAddress = 12

$Title = 14

$Company = 15

$Phones = 27

$FirstName = 37

$LastName = 38

 

$cl = [Microsoft.Lync.Model.LyncClient]::GetClient()

 

$gs = $cl.ContactManager.Groups

 

foreach ($g in $gs)

{

    Write-Host

    $g.Name

   

    foreach ($contact in $g)

    {

        $contact.GetContactInformation($LastName)

        $contact.GetContactInformation($FirstName)

        $contact.GetContactInformation($Title)

        $contact.GetContactInformation($Company)

        $contact.GetContactInformation($PrimaryEmailAddress)

        $eps = $contact.GetContactInformation($Phones)

           

        foreach ($ep in $eps)

        {

            switch ($ep.Type)

            {

                “WorkPhone” {“work: ” + $ep.DisplayName}

                “MobilePhone” {“mobile: ” + $ep.DisplayName}

                “HomePhone” {“$home: ” + $ep.DisplayName}

             }

        }

           

    }

}

 

Yes, we did say this is the simplified version. But don’t worry, we’ll walk through it and you’ll see it’s not nearly as complicated as it might seem at first.

 

The first two lines are required anytime you’re going to be working with the Lync SDK:

 

$assemblyPath = “C:Program Files (x86)Microsoft LyncSDKAssembliesDesktopMicrosoft.Lync.Model.DLL”

Import-module $assemblyPath

 

The first line sets a variable ($assemblyPath) to the full path of the DLL that contains the objects for the Microsoft.Lync.Model namespace. A namespace is simply a container for a bunch of objects, methods, and properties. (Stay with us here just a minute longer, it gets easier.) When we create objects (such as Lync Contacts) that are part of an SDK, Windows PowerShell doesn’t know where to get those objects from until you tell it which SDK, and which namespace, the object is in. The path we’ve provided here is the default installation path of the Lync SDK, and the Microsoft.Lync.Model.DLL file contains all the objects for the Microsoft.Lync.Model namespace. If you didn’t install the SDK to the default path you’ll need to modify this line.

 

That was probably more explanation than you needed, but it’s there in case you wanted to know. All you really need to know is that you have to include the full path to the DLL for this script to work, and you need to pass that DLL path to the Import-Module cmdlet:

Import-Module $assemblyPath

 

Import-Module is a Windows PowerShell cmdlet that reads the DLL and enables you to use all the objects, methods, and so on that are in that DLL.

 

Okay, we now have everything from the Lync SDK that we need to actually get started. The next thing we do is define some variables:

 

$DisplayName = 10

$PrimaryEmailAddress = 12

$Title = 14

$Company = 15

$Phones = 27

$FirstName = 37

$LastName = 38

 

We’ll get back to these in a bit. For now we’re going to move on:

 

$cl = [Microsoft.Lync.Model.LyncClient]::GetClient()

 

This line grabs the instance of Lync 2010 currently running on your machine and puts it in the variable $cl. It does this by calling the GetClient method of the Microsoft.Lync.Model.LyncClient class.  (This class is part of the Lync SDK, so the script would crash at this line if we hadn’t imported the DLL with the Import-Module cmdlet.) Now that we have a reference to your Lync client stored in $cl, we need to access all the contact groups (such as Frequent Contacts and Current Contacts). We do that with this line:

 

$gs = $cl.ContactManager.Groups

 

In this line we retrieve the Groups collection, which is available through the client’s ContactManager object. The Groups collection contains just what it sounds like: all the group you have in your instance of Lync. We store the collection of groups in the variable $gs.

 

Now all we need to do is go through all the groups and pull out the information for each contact. To do that we set up a foreach loop to loop through the groups collection and look at one group at a time:

 

foreach ($g in $gs)

{

    Write-Host

    $g.Name

 

We start the loop by saying that for every group ($g) in the groups collection ($gs) we’re going to display a blank line, then the name of the group ($g.Name). (The Write-Host cmdlet, with no parameters or values, will write a blank line.) We don’t really need to do this to display contact information, but we thought it would be helpful to know which group each contact is in.

 

For each group, we want to get information for every contact within that group. What do you think we need to do next? That’s right, we need another foreach loop. (The For each at the beginning of the sentence gave it away, didn’t it?)

 

foreach ($contact in $g)

 

Each group is really a collection of contacts. So for each group ($g) we go through and retrieve information about one contact ($contact) at a time. In order to retrieve information about a contact, you call the GetContactInformation method:

 

        $contact.GetContactInformation($LastName)

        $contact.GetContactInformation($FirstName)

        $contact.GetContactInformation($Title)

        $contact.GetContactInformation($Company)

        $contact.GetContactInformation(

Comments (8)
  1. Eric says:

    What are our options for backing up contact data from the entire Lync back-end database?  In OCS 2007 R2, we used DBImpExp.exe as part of our DR plan.  This doesn't seem to be an option in Lync 2010.  

  2. Craig says:

    I would like to import my contacts into the client via a csv file.

  3. Marco says:

    Hi,

    is there a way to import Contacts to Lync?

    Especially including the Name of the person?

    I need this to import many YahooIDs to a Lync Client for some hounded users

Comments are closed.