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 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.
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
Script to Create a Contact in your Active Directory
' Contact .vbs
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.
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.
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.
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
I like the Permissions 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!
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=" _
Set objContact = objContainer.Create("Contact","cn=" _
Note 1: Observe how we open and close the Excel spreadsheet by manipulating the objExcel object. (Starting at line 27).
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",_
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