Import Email addresses from CSV file into Active Directory Windows 2003

Posted on 2009-02-11
Last Modified: 2012-05-06

I have a CSV file that containing Last Name, First Name , User Name , Email data.
Last Name, First Name and User Name are already in the AD and in use, but i need to add the Email part without generating more User accout problem :o(

Any script or command syntax that I can use?

Best regards,

Question by:fjebja
    LVL 70

    Expert Comment

    by:Chris Dent


    With Exchange or without?

    LVL 1

    Author Comment

    Hi Chris,

    It's without Exchange, we are using Lotus Notes.
    LVL 70

    Accepted Solution


    Great, just didn't want to break that :)

    The following VbScript (save as .vbs) takes a simple CSV file in the following format:


    And imports the Email into the mail field (as seen on the general tab in AD Users and Computers) for each Username.

    I recommend you test it on a very small set of users before feeding it the entire CSV. Do let me know if the CSV file also includes quotes, that'll take a little more effort.

    Option Explicit
    Const FILE_NAME = "TheFile.csv"
    Function GetObjectDN(strObject, strDomain)
      ' Return Type: String
      ' Returns a Distinguished Name for an Object from it's NT SAM ID.
      ' This will only function for valid object types within an NT Domain structure.
      Const ADS_NAME_INITTYPE_GC = 3
      Const ADS_NAME_TYPE_1779 = 1
      Const ADS_NAME_TYPE_NT4 = 3
      Dim objNameTranslate
      Dim strObjectDN
      On Error Resume Next : Err.Clear
      Set objNameTranslate = CreateObject("NameTranslate")
      objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
      objNameTranslate.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strObject
      strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
      If Err.Number <> 0 Then  ' Make the DN Blank for a Failed Search
        strObjectDN = ""
      End If
      Set objNameTranslate = Nothing
      On Error Goto 0
      GetObjectDN = strObjectDN
    End Function
    Dim objFileSystem : Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Dim objFile : Set objFile = objFileSystem.OpenTextFile(FILE_NAME, 1, False, 0)
    Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
    Dim strDomain : strDomain = objNetwork.UserDomain
    Do Until objFile.AtEndOfStream
      Dim arrLine : arrLine = Split(objFile.ReadLine, ",")
      Dim strUsername : strUsername = arrLine(2)
      Dim strEmail : strEmail = arrLine(3)
      Dim strDN : strDN = GetObjectDN(strUsername, strDomain)
      If strDN <> "" Then
        Dim objUser : Set objUser = GetObject("LDAP://" & strDN)
        objUser.Put "mail", strEmail
        Set objUser = Nothing
      End If

    Open in new window

    LVL 1

    Author Closing Comment

    Thank you, that worked perfectly!

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now