Import contacts from a csv file using VbScript

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:

cscript ContactImport.vbs -f <file Name> -o "<organisational Unit Path>"
    [-r] [-s] [-a <attributes>] [-d <delimiter>] [-n "<name Format>"] [-t]

  -f <file Name> - Input File Name (Required)
  -o <organisational Unit Path> - Target OU for Contact Objects (Required)
  -r - Read the Header Line as AD Fields
  -s - Skip the Header Line
  -a <attributes> - AD Attibutes to write Fields to
    (Required if not using Header)
  -d <delimiter> - File Delimiter. Uses comma if not specified.
  -n <name Format> - 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:

givenName,sn,TargetAddress,streetAddress,streetAddress
Chris,Dent,chris.dent@somedomain.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.

cscript ContactImport.vbs -f Contacts.csv -o "OU=Contacts,DC=somedomain,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:

Chris,Dent,chris.dent@somedomain.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.

cscript ContactImport.vbs -f Contacts.csv -o "OU=Contacts,DC=somedomain,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:

FirstName|LastName|Address
Chris|Dent|chris@somedomain.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.

cscript ContactImport.vbs -f Contacts.txt -o "OU=Contacts,DC=somedomain,DC=example" -a "givenName,sn,targetAddress" -s -d "|"

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

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

4 Responses to this post.

  1. Posted by Jake on 21.10.08 at 6:25 pm

    Hi Chris

    This looks like a brilliant script.

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

    Thanks
    Jake

  2. Posted by Chris on 21.10.08 at 6:25 pm

    Hey Jake,

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

    All the best,
    Chris

  3. Posted by Ken K. on 21.10.08 at 6:25 pm

    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²

  4. Posted by Ken K. on 21.10.08 at 6:25 pm

    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

Respond to this post