Download ContactImport.vbs

This script will create contacts in Active Directory based on a formatted CSV file. It was written to allow automatic synchronisation of contacts using a CSV file as the source.

The usage for the script will echo when run without any parameters. Here’s a summary:
[code lang=”plain”]
cscript ContactImport.vbs -f “” -o “
[-r] [-s] [-a ““] [-d ““] [-n ““] [-t]

-f “” – Input File Name (Required)
-o “” – Target OU for Contact Objects (Required)
-r – Read the Header Line as AD Fields
-s – Skip the Header Line
-a “” – AD Attibutes to write Fields to (Required if not using Header)
-d “” – File Delimiter. Uses comma if not specified.
-n “” – DisplayName and CN Format by AD Attributes. Uses TargetAddress if not specified.
-t – Test Only

Usage examples

Reading data from a CSV using the header line as fields

Example CSV file data:
[code lang=”plain”]
givenName,sn,TargetAddress,streetAddress,streetAddress
Chris,Dent,chris.dent@domain.example,123 Somewhere Street,London

The following command will read the header line as the fields to import, then create contacts using givenName and sn as the display name / name.
[code lang=”plain”]
cscript ContactImport.vbs -f Contacts.csv -o “OU=Contacts,DC=domain,DC=example” -r -n “givenName sn”

The attribute streetAddress appears twice, it will be concatenated using a comma as a delimiter. The streetAddress attribute will contain “123 Somewhere Street, London” in this example.

Defining attributes on the command line

Example CSV file data:
[code lang=”plain”]
Chris,Dent,chris.dent@domain.example,SomeCorp,SomeOffice

The following command will read the file, then import attributes based on the -a value which must reflect the order of attributes in the import file.
[code lang=”plain”]
cscript ContactImport.vbs -f Contacts.csv -o “OU=Contacts,DC=domain,DC=example” -a “givenName,sn,targetAddress,company,physicalDeliveryOfficeName”

Because no displayName format has been specified the targetAddress field will be used.

Using a custom file delimiter

Example file data:
[code lang=”plain”]
FirstName|LastName|Address
Chris|Dent|chris@domain.example

In this example the header line contains names that cannot be used as attributes in Active Directory so the script is instructed to ignore it with -s. The fields are defined instead using the -a option. The Delimited is specified with -d.
[code lang=”plain”]
cscript ContactImport.vbs -f Contacts.txt -o “OU=Contacts,DC=domain,DC=example” -a “givenName,sn,targetAddress” -s -d “|”

As above, because no displayName format is specified the targetAddress is used.

20 Comments

  1. Hi Chris

    This looks like a brilliant script.

    Do you perhaps have an example csv file that I can look at?

    Thanks
    Jake

    Reply

  2. Hey Jake,

    I’ve added a few examples, is that enough to get you going?

    All the best,
    Chris

    Reply

  3. Hello Chris,

    I spent a good bit of time trying to locate a well-written, flexible, VBScript to automate Contact creation. I think yours is the best I’ve found.

    -k²

    Reply

  4. Hello Chris,

    I have a question regarding “dangerous” Contact object LDAP attributes: Do you happen to know which LDAP attributes for Contacts can and cannot be imported?

    It is fairly obvious that some attributes should not be imported: uSNCreated and uSNChanged for example. No big deal there—there’s no obvious advantage to doing so that I am aware of. That said, there are a number of other attributes that are more ambiguous.

    My interest in this is general purpose – don’t want to mess anything up. More specifically though, we’d like to be able to import Contacts and have them mail enabled immediately. It’s not always clear which attributes we need to do so.

    Any advice or direction would be greatly appreciated.

    Thanks,
    Ken

    Reply

  5. I realize this is a few years out, but I had to say I ran this script to import a long list of names and e-mail addresses for an Exchange distribution group and it worked wonderfully. Thanks for all the hard work!

    Reply

  6. Hi Chris,
    3 years later and still posting about this script 🙂
    I agree with everyones comments, this script works rather damn well. Thankyou!

    I did have one interesting issue with it though.
    In my import CSV if I ran with givename as the first column, for some reason my display name when using the -n switch and specifying givenname (or givenname sn – company) would concatenate both the sn + givenname (bloggsjoe) for the Displayname (bloggsjoe bloggs – company).
    If you change the first column to something other than Givenname first (I used company) it works perfectly.
    I am not quite sure how this is happening or why, I spent a bit of time looking through the code but just couldn’t pick where it was doing it.

    Cheers,
    Steve

    Reply

    • That’s a little unexpected. Would you be able to provide a sample of the file you’re using? I tried to duplicate with this one but cannot reproduce the bug:

      givenname,sn,company,targetAddress
      bob,hope,somecompany,bob@hope.com

      Associated command to execute the script:

      cscript ContactImport.vbs -f test.csv -o “OU=Somewhere,DC=domain,DC=example” -r -t -n “givenname sn – company”

      Reply

  7. Hi Chris,
    For the life of me I have not been able to replicate the issue again. I have tried a few different combinations with the givenname first but they have all worked successfully.

    There is another question I have in regards to Proxyaddresses. Is it possible to import multiple proxyaddresses with the script? For instance I need to be able to include the standard SMTP proxy address but also an X500 address. I have tried using a few different delimiter types but so far I haven’t had any luck.

    Cheers,
    Steve

    Reply

  8. Hmm in theory it might work if you call the field proxyAddresses, and use the modified CheckUpdate Subroutine below. Ensure you include the address type prefix (e.g. smtp:bob@domain.example X500:/somevalue/).

    Afraid I can’t test it, but if you’d like to take the update for a spin? 🙂

    Chris

    Reply

  9. Amazing! Thank you so much!
    I will give it a go today!

    Cheers,
    Steve

    Reply

  10. Hi Chris,
    I have given it a go with some success. I have replaced the separator with ; for the proxyaddresses and importing multiple SMTP addresses works correctly. When entering in a X500 address with the format like:
    X500:/O=FCL/OU=AUST/cn=Recipients/cn=TestContact the import doesn’t work.
    This is the log entry for it:
    Appending proxyaddresses Attribute: aaEmail aaMigration – company: New: /o

    The problem I think is this proxyaddresses is also delimited by = which is breaking up the X500 address.
    So I think before it even gets to the adding proxyaddresses the string is broken.

    I am working through it here to see if I can get it to parse the address correctly but any assistance would be great!

    Cheers,
    Steve

    Reply

  11. Chris!
    I got it working. I am not sure just how elegant it is but it definitely works. When reading the file you match the attribute with the value using = then later on you split it using = for creating and updating the contact.
    All I did was replace the = with ^ in the 4 locations needed in the script and bing off we go.

    Thank you so much for spending your own time in coming up with the Proxyaddress update. It has saved me so much time and pain.

    Cheers,
    Steve

    Reply

  12. one other thing that is important if exchange is to send email to these contacts is the field internetEncoding, which needed to be set to 1310720 in order to work..

    Reply

  13. This script is fantastic.

    If your domain name changes, update your csv file and then rerun this script. It will update all the contacts.

    Absolutely fantastic.

    Reply

  14. When I run your script, the content of the field TargetAddress will be added as display name instead in the the mail field. What’s wrong? How can I change the script that the email adresses of the contacts will be stored in the mail field?

    Reply

    • The mail field is implicitly populated by the act of mail-enabling the contact. The mail field should fill in the next time the recipient update service gets its hands on the contact.

      Chris

      Reply

  15. thanks Chris
    wonderful script !

    Reply

  16. Great Work man! THank you for sharing.

    However I get an exception, It is not importing properly when the CSV data contains special character, for example: ã.

    I have tried to save the csv in the UTF8 format but without success. Any idea where I need update?

    ps.: You need have installed the Exchange Tools installed to get run the script successfully!

    Thank you very much.

    Reply

  17. People,

    I have saved the csv file in ASCII format and it is working proporly!

    Thank you

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *