Script to import user photos into Active Directory

After recently installing Lync 2010, I soon realised that I should probably have already imported user photos into Active Directory, seeing as Outlook 2010 also supports user thumbnails and it would be a nice little feature to add in.

I found a vb script here that worked very well, In fact I spotted the same script in a few places.

I saved 96 x 96 pixel jpegs photos (~15k) of each user in “C:\UserPhotos” on a DC. Each photo was saved as Firstname<space>Surname.jpg, to match the users name in AD.

This is the code for ImportPhotos.vbs:

Const ForReading = 1
InDir = “C:\UserPhotos”
Set fso = CreateObject(“Scripting.FileSystemObject”)
set oIADS = GetObject(“LDAP://RootDSE”)
strDefaultNC = oIADS.Get(“defaultnamingcontext”)
Set theConn = CreateObject(“ADODB.Connection”)
theConn.Provider = “ADsDSOObject”
theConn.Open “ADs Provider”
Set theCmd  = CreateObject(“ADODB.Command”)
theCmd.ActiveConnection = theConn
Set objRecordSet = CreateObject(“ADODB.Recordset”)
For Each tFile In fso.GetFolder(InDir).Files
    tName = tFile.Name
    ‘Gets the persons Name from the file by stripping the extention.
    tName = Left(tName, InStrRev(tName,”.”)-1)
    ‘You may need to tweak this bit depending on your naming conventions.
    strQuery = “<LDAP://” & strDefaultNC & “>;” & _
                              “(&(objectClass=person)(name=” & tName & “));name,adspath;subtree”
    theCmd.CommandText = strQuery
    Set objRS = theCmd.Execute
    If objRS.RecordCount = 0 Then
      MsgBox “Can’t find account for ” & tName
    Else
      Set objUser = GetObject(objRS(“adspath”))
      ObjUser.Put “jpegPhoto”, ReadByteArray(tFile.Path)
      ObjUser.SetInfo
    End If
Next
‘Stolen from http://www.ericphelps.com/q193998/index.htm
Function ReadByteArray(strFileName)
    Const adTypeBinary = 1
    Dim bin
    Set bin = CreateObject(“ADODB.Stream”)
    bin.Type = adTypeBinary
    bin.Open
    bin.LoadFromFile strFileName
    ReadByteArray = bin.Read
End Function

Once ran, you can check the value of thumbnailPhoto using ADSIEdit.

The script will run through and update any user in AD that it finds a photo for, it will also pop up a message box of any errors. You can then just add more photos down the line and then re-run the script.

Once done, you may also want to update the Offline Address Book in the Exchange Powershell by running:

Get-OfflineAddressBook | Update-OfflineAddressBook

Now you should see user photos appearing in Lync and Outlook etc.

  • Shai

    Thank you very much :) !

    • dphuk

      No worries :)