How to Create a Contact in Active Directory with VBScript

Tutorial for Creating Contacts with a VBScript

Creating Contact objects with a VBscript can be hard or it can be easy.  The difference lies in whether you want to create the object solely in Active Directory, or whether you want to build Contacts for use in an Exchange Organization.

In order to get a feel for the scripting tasks, create a Contact manually through Active Directory Users and Computers.  If Active Directory only asks for two pieces of information, name and email address, then the script will be straightforward.  However, if the wizard asks for Exchange 2003 information such as Information Store, then that is your cue that scripting will require extra fields, for example, legacyExchangeDN, mailNickname and targetAddress.

On this page, I will show you two examples of scripts that build a Contact with just a name and an email address.  The first VBScript is straightforward.  The real challenge comes in the second script where we want to bulk import Contacts by extracting their names and addresses from an Excel Spreadsheet. 

Note: On the next page I have a tutorial for creating a Contact in an Exchange 2003 Organization.

Topics for VBSCript Contacts


Our Mission and Goal

Our goal is to create Contacts.  Compared with creating Users, the first stage will be easy, because all we need is the name and email address.  Unlike most other Active Directory objects, contacts do not require the sAMAccountName attribute.

The second stage introduces a more realistic scenario, we have a list of suppliers along with their email addresses in an Excel spreadsheet.  Our script will open the spreadsheet and read the column containing the names.  This method is often called bulk-import.

Example 1 – Script to Create a Contact in Active Directory


Recommended: You logon as administrator, preferably at a domain controller.  Remote Desktop would be a suitable alternative.  If that is not possible, you could get these sample scripts to work from an XP machine as a non-administrator, but why introduce extra complications?  At the beginning you want easy success, with fewest obstacles.

Instructions for Creating a Contact in Active Directory

  1. You need access to a Windows Active Directory domain.
  2. Copy and paste the example script below into notepad or a VBScript editor.
  3. Decide what value to use for strContainer.  Where is your spreadsheet?
  4. Change the values of the variable strEmail to a Contact that you know.
  5. Save the file with a .vbs extension, for example: contact .vbs
  6. Double click contact .vbs.  Open Active Directory Users and Computers and check the strContainer container for strContactName.  If necessary, ‘Refresh’ the contents of your OU.

Script to Create a Contact in your Active Directory


‘ Contact .vbs
‘ Purpose VBScript to create a contact object in Active Directory
‘ Author Guy Thomas
‘ Version 1.3 – May 2010
‘ ———————————————–‘
Option Explicit
Dim objRoot, objOU, objDomain, objContact, strYourDescription
Dim strDNS, strContainer, strContactName, strEmail

‘ Set string variables
strContainer = "OU=Suppliers"
strContactName = "cn=MySupplier1"
strEmail = ""
strYourDescription = "Guy’s Contact"

‘ Section to attach to Active Directory
Set objRoot = GetObject("LDAP://rootDSE")
strDNS = objRoot.Get("defaultNamingContext")
Set objDomain = GetObject("LDAP://" & strDNS)

‘ Section to create the contact
Set objOU = GetObject("LDAP://"& strContainer & "," & strDNS)
Set objContact = objOU.Create("contact", strContactName)
objContact.Put "Description", strYourDescription
objContact.Put "Mail", strEmail

Wscript.Echo "Look in " & strContainer & " for (F5) " & strEmail

‘ End of Sample Contact VBScript

VBScript Tutorial – Learning Points

Note 1:  The header section, in the first 10 lines, explains the purpose of the script and declares the variables.

Note 2:  The simple, but clever command, which allows the script to work with any domain is: GetObject("LDAP://rootDSE").  Crucial, this statement binds WSH / VBScript to Active directory.  The next line puts the focus on the OU=Suppliers container, as that is where the Contact will be born.

Note 3: .Create is a method which builds an object.  In this instance, see how we use "Contact" not "User" or "OU".

Note 4:  Every object, even Contacts, must have a cn (common name).  In this example, we use strContactName.  The other key attribute is the Mail property, trace how the script supplies the information with strEmail.

Guy Recommends:  SolarWinds’ Free Bulk Mailbox Import ToolFree Download Bulk Mailbox Import Tool

Import users from a spreadsheet, complete with their mailbox.  Just provide a list of the users with the fields in the top row, and save as .csv file.  Then launch this FREE utility, match your Exchange fields with AD’s attributes, click and import the users.  Optionally, you can provide the name of the OU where the new mailboxes will be born.

There are also two bonus tools in the free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users and mailboxes into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk mailbox import tool.

Example 2: Sample Script to Create Contacts from Spreadsheet Data

We are now ready to bulk import Contacts from a spreadsheet.  Just for testing, I create only 3 or 4 sample contacts because I often want to delete them, modify the script and try another run.

Prerequisites for Creating Contacts from a Spreadsheet

Each Contact will occupy one row of the spreadsheet, for example Joshua Kidby in Row 3. Each attribute will always be in the same column, for example, everyone’s Mail address is in Column B.

Sample Excel spreadsheet with data for VBScript contacts

Create a spreadsheet with your prospective Contacts’ properties.  My advice is to spend time researching the LDAP attributes, which correspond to what you see in an Active Directory Users and Computers property sheet.  You can download my Spreadsheet here

Be aware that where you save this .xls file should correspond to the strPathExcel variable in the script below.  For example, Line 16 : E: \scripts\ContactExcel .xls.

‘ ContactExcel .vbs
‘ Purpose VBScript to create contacts from a list on names in Excel
‘ Usage where you need contacts for example, suppliers
‘ Author Guy Thomas
‘ Version 2.2 – May 2010
‘ ———————————————–‘

Option Explicit
Dim objRootLDAP, objContainer, objContact, objExcel, objSheet
Dim strOU, strContactName, strPathExcel, strEmail
Dim intRow, strYourDescription, strFirst, strLast

‘ Set string variables
‘ Note: Assume an OU called suppliers exists.
strOU = "OU=Suppliers ," ‘ Note the comma
strPathExcel = "E:\Scripts\contacts.xls"
strYourDescription = "Guy’s Contact"
intRow = 3 ‘ Row 1 contains headings

‘ Section to bind to Active Directory
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU _
& objRootLDAP.Get("DefaultNamingContext"))

‘ Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)

‘ Here is the loop that cycles through the cells
Do Until (objExcel.Cells(intRow,1).Value) = ""
   strContactName = objExcel.Cells(intRow, 1).Value
   strEmail = objExcel.cells(intRow, 2).Value
   strFirst = objExcel.cells(intRow, 3).Value
   strLast = objExcel.cells(intRow, 4).Value
   ‘ Build the actual contacts.
   Set objContact = objContainer.Create("Contact",_
   "cn=" & strContactName)
   objContact.Put "Mail", strEmail
   objContact.Put "givenName", strFirst
   objContact.Put "sn", strLast
intRow = intRow + 1
objExcel = Nothing

‘ End of Sample ContactExcel VBScript

Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource.  Give this permissions monitor a try – it’s free!

Download SolarWinds’ Free Permissions Analyser – Active Directory Tool

VBScript Tutorial – Learning Points

Note 0:  Brian K suggests the following amendment to Line 36

You might want to change the line in the ContactExcel.vbs script as below to handle a "," in the CN


Set objContact = objContainer.Create("Contact","cn=" _

& strContactName)


Set objContact = objContainer.Create("Contact","cn=" _

& Replace(strContactName,",","\,"))

Note 1:  Observe how we open and close the Excel spreadsheet by manipulating the objExcel object. (Starting at line 27). 
Set objExcel = CreateObject("Excel.Application")
Set objSheet = objExcel.Workbooks.Open(strPathExcel)

Note 2:  The Contact’s name and email address are stored in the .cell property of the Excel Workbook.

Note 3:  Trace the data in the spreadsheet to lines 31-35 in the VBScript.  At first, it is confusing the way that Column A hold values for the CN, but the VBScript referenced Column A as intRow, 1.  Once you realize that the 1 (in intRow,1), refers to Column A, and  intRow,2 would be Column B, then the picture becomes clearer.  Do go back over this method as it is the cornerstone for so many spreadsheet / VBScript interactions.

Note 4:  The Contact object is build with command: objContainer.Create("Contact",_
"cn=" & strContactName). The next 3 lines, (39-41) add values such as strEmail to the Contact.  Finally, .SetInfo is rather like pressing the OK button in Active Directory Users and Computers.

Summary for Creating Contacts

The best way to create Contacts is from a list of names in a spreadsheet.  From a learning point of view, it is best to investigate how to create a single Contact, then progress to the Excel Spreadsheet method.  As with other VBScripts, this scripts mimics what you would do manually if you needed to create a Contact in Active Directory Users and Computers.

See More Active Directory VBScripts featuring contacts

• User Spreadsheet  • LDAP Properties  • Create Users  • Bulk Import User Ad  • Ezines

Ezine 23 Enable Accounts  •VBScript Exchange Contact  •VBScript Create Contact  • Tool Kit

Ezine 42 LDAP  • Ezine 44 CSVDE  •Ezine 53 Contacts  • Ezine 81 Contacts  • Ezine 82 Contacts