How to Create a User’s Mailbox with VBScript

Tutorial for Creating a User’s Mailbox with VBScript

This page covers not only an example script to create a mailbox, but also the strategy and tactics you need to achieve this complex task.  In fact, the secret is to have a spreadsheet full of the correct LDAP attributes needed to add the 4 tabs to user’s Active Directory property sheet.

Topics for Creating a User’s Mailbox with a VBScript

I imagine that the situation is this, you have just installed Exchange 2003 and now you are turning your attention to the hundreds of new joiners who each need an Exchange Mailbox. There is no hiding from the fact that you face a difficult pains-taking task.  This page will help you break down the task of creating a mailbox into stages, get each stage working, then bolt them together to create a final script.


Our Mission and GoalExamples of Mailbox-enabled user Exchange Properties VBScript

The goal of this page is to script new mailbox-enabled users.  What this script does is add 4 tabs to the User’s property sheet.  The benefit is that not only can they logon but also they can send and receive email from your Exchange server.

What is your knowledge of Exchange?  Do you need a refresher with the Exchange System Manager?  Are you familiar with the Exchange organization structure in general, and the mailstore names in particular?  See more on configuring Exchange here.

Good news, I have a separate script to show you how to create a user with VBScript. (See here to create a user ).  That leaves this page free to concentrate on building the 5 extra LDAP attributes, which combine to build the user’s mailbox.  The three factors, which combine to make building mailboxes a high error task are:

1) A surprising number of people do not know the names of their Exchange Organization. 

2) People confuse DNS names with Active Directory domain names, they are not always one and the same.

3) Confusion over the LDAP properties, in particular the relationship between the columns in the spreadsheet and the .cell properties in the VBScript.

Here is a common example to illustrate my theme – attention to detail.  For instance, assume that your Exchange Organization is called TopMail.  However, the Exchange System Manager displays TopMail (Exchange).  What do you script?  Does it matter if you use TopMail or TopMail (Exchange)?  The answer is yes it makes a difference, and only plain TopMail would work.

Example – Method to Create a Mailbox-Enable User

Without wishing to confuse you, there are two possible strategies for creating a user’s mailbox.  Either you can ask the script to read a single long value from one cell of the spreadsheet, or you can build that long value from commands actually in the VBScript.  Either way, it requires attention to detail both for the structure of the field and the correct names for all the parts.

If (when) your script does not produce the desired results, try the fall back technique of creating a user manually, then using ADSI Edit to check the values of the LDAP fields listed below.

Spreadsheet Preparation

The aim of this part of the mission is to create the values for the imported users in a spreadsheet.  Once these values are correct, we open the spreadsheet from the VBScript and read the LDAP values needed to build the user account, including their mailbox.

Let us examine the 5 Mailbox specific attributes that we must script.  However, before any scripting, as a matter of tactics, I would create a mailbox-enabled user manually.   Next, I would  export Active Directory using CSVDE -f exch.csv and examine the LDAP attributes and values of that newly-created user account.

msExchHomeServerName – Exchange needs to know the name of our Exchange server so that it can deliver the email, here is an example:

/o=CPNou/ou=First Administrative Group/ cn=Configuration/cn=Servers/cn=OurServer

The two components of msExchHomeServerName that you definitely need to change are, the first (o=CPNou) and last (/cn=OurServer).

/o=CPNou.  Change this to the precise name of your exchange organization, you do not need the (Exchange) suffix.  Action –> Check your Exchange System Manager, and or, ADSI Edit.

/cn=OurServer.  What is the name of your Mailbox server?  Check with the Exchange System Manager.

/ou=First Administrative Group.  This is likely to be exactly the same in your situation, but do double check.

/cn=Configuration/cn=Server.  Almost certainly, this section will be the same on your test user.

Note: The msExchHomeServerName attribute does not require the mailbox name, that is covered in the homeMDB attribute.  I cannot emphasise enough, that for successful scripting, or more likely, successful troubleshooting you must take the time to understand what each section of the attribute is doing.

mail – Here is the classic email address format, name@domain.  Assuming you are using an Excel Spreadsheet to hold the account values, you can almost certainly calculate the correct value for the ‘name’ part of the address from the givenName and sn, for example, = givenName &" " & sn.  There are numerous variations for example, a dot instead of a spaces or shortening the sn with the left(text,4) function.  Remember that the domain part is the email domain, which may, or may not be the same as the DNS domain.  Perhaps you are beginning to see what I mean by attention to detail.

You may also wish to investigate targetAddress.  The format is always smtp: name@domain.  In this instance smtp is case sensitive, SMTP: meaning the default email address, smtp: meaning an alternative recipient address.

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.

mailnickname – I advise setting mailnickname = sAMAccountName (Pre-2000 Logon Name).  Again, derive this LDAP attribute with Excel’s functions. For example =D2, rather than hard-coding guyt.  In this example I assume that D2 already holds the value guyt.

mDBUseDefaults – A easy field to script, just the value to: TRUE

homeMDB – Research the correct name of the user’s Mailstore on the server.  Observe the test user, or launch the Exchange System Manager.  HomeMDB is very difficult to script.  The only way that I succeed was to break the task into a dozen or so steps.  Here is the end result:

CN=GuyStore,CN=First Storage Group,CN=InformationStore,CN=PARIS,CN=Servers,
CN=First Administrative Group,CN=Administrative Groups,CN=CPNou,CN=Microsoft Exchange, CN=Services,CN=Configuration,DC=cpexch,DC=com

Here is how I bolted the steps together.

Try SolarWinds Free CSVDE Import Tool ยป

Instructions for Creating a Mailbox-Enabled User.

  1. You should run this VBScript on a Windows Active Directory domain.
  2. Copy and paste the example script below into notepad or a VBScript editor.
  3. Inspect and change the values of strxyz values.
  4. Save the file with a .vbs extension, for example: homeMDB .vbs.
  5. Double click homeMDB .vbs and check string in the message box.

Script to Create the homeMDB attribute for the Mailbox


‘ homeMDB .vbs
‘ Sample VBScript to create the LDAP string for the mailboxes
‘ Author Guy Thomas
‘ Version 1.4 – May 2010
‘ ————————————————–‘
strServer = "Paris"
strExchangeOrg = "CPNou"
strDomain = "DC=CPEXCH,DC=COM"

‘ These are the defaults, compare with your server and amend.
strAdminGroup = "First Administrative Group"
strStorageGroup = "First Storage Group"
strStoreName = "Mailbox Store (" & strServer & ")"

‘ Build LDAP string
strMailbox = strMailbox & "CN=" & strStoreName
strMailbox = strMailbox & ",CN=" & strStorageGroup
strMailbox = strMailbox & ",CN=InformationStore"
strMailbox = strMailbox & ",CN=" & strServer
strMailbox = strMailbox & ",CN=Servers"
strMailbox = strMailbox & ",CN=" & strAdminGroup
strMailbox = strMailbox & ",CN=Administrative Groups"
strMailbox = strMailbox & ",CN=" + strExchangeOrg
strMailbox = strMailbox & ",CN=Microsoft Exchange"
strMailbox = strMailbox & ",CN=Services"
strMailbox = strMailbox & ",CN=Configuration"
strMailbox = strMailbox & "," & strDomain

WScript.Echo "Mailbox " & vbCR & strMailbox


‘ End of Sample User Mailbox VBScript.

VBScript Tutorial – Learning Points

Note 1:  Once the script produces the required value for the homeMDB, then either copy this value into a cell of your spreadsheet, or else retain this block in your VBscript to create a mailbox-enabled user.

Note 2: The secret of success is to double check each element, especially the strExchangeOrg and strDomain.

An alternative would be to use PowerShell Set-Mailbox.

Summary of Creating a User’s Mailbox

Creating a mailbox-enabled user is a specialist task.  The secret is to breakdown the complex task of building the mailbox into a series of manageable steps.  Bear in mind that the whole mission is to create a fully functional user object with 17/18 tabs, the purpose of this page is to create just the 4 Exchange specific tabs.  The problem we have to overcome is this, it is more difficult to build these 4 Exchange tabs than all the other 13 tabs put together.


See Also

Create user Accounts               • Set Password for user account