VB Script to set email in active directory

By jtbjustin ·
I have a Windows 2008 Active Directory server, and within that server I have about 800 user accounts. All of these accounts have user names that match their work issued email account, less the domain name. So for example, my login name is jtbjustin, and my email is

So here is what I would like to do,
I would like to write a VB script that takes someone's active directory user name and then puts that plus "" into the email contact field in active directory.

I also want to run this script on only a certain OU, I don't want to run it for every user in my domain.

Could any one give me some documentation or help to write this kind of script? I have virtually no experience with VB.

Thank you very much!

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

I haven't tested this with 2008

by Jacky Howe In reply to VB Script to set email in ...

I've only used it on SBS 2003. Check the link to Guy's site as he may have updated information.

I normally use Guy Thomas's scripts as they are easy to understand and as long as the users have an account in Active Directory and they are in the specified OU they will get a Mailbox. Modify the below text to suit.

' VBScript to add mailboxes to existing users
' Author Guy Thomas
' Version 2.6 - April 8th 2005
Option Explicit
Dim ADOcon ' ADODB.ADOconnection
Dim command ' ADODB.Command
Dim rs ' ADODB.Recordset
Dim objPerson ' CDO.Person
Dim mbox ' CDOEXM.IMailboxStore interface
Dim iMsg ' CDO.Message
Dim intMailbox ' Increment the number of mailboxes created

strServer = "fs2003" ' Name of the File Server
strExchangeOrg = "MINERO" ' Domain Name
strDomain = "DC=MINERO,DC=LOCAL"
strOU = "OU=SBSUsers" ' Container for the users that will get a Mailbox
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

' Create the ADODB.ADOconnection and ADOconnect to Active Directory
Set ADOcon = CreateObject("ADODB.connection")
ADOcon.Provider = "ADsDSOObject"

' Create and configure the ADODB.Command
Set command = CreateObject("ADODB.Command")
Set command.Activeconnection = ADOcon

' Select all properties of users from a given organizational group
command.CommandText = "SELECT * FROM 'LDAP://" & strOU & "," _
& strDomain & "' WHERE objectClass = 'user'"

' Execute the ADODB.Command and store the results in the ADODB.Recordset
Set rs = command.Execute

' Preparing the mailbox
Set objPerson = CreateObject("CDO.Person")

' Loop through the users
intMailbox = 0
While Not rs.EOF
' Open each CDO.Person using the location information specified by the ADsPath property
objPerson.DataSource.Open (rs.Fields("ADsPath").Value), , 3
' Get the IMailboxStore interface to the users mailbox
Set mbox = objPerson.GetInterface("IMailboxStore")

'Test if the mailbox already exists
If mbox.HomeMDB = "" Then
' If the mailbox does not exist, create it

mbox.CreateMailbox strMailbox
intMailbox = intMailbox + 1
' Set the Email property of the Person to the new mailbox
strEmail = objPerson.Fields("sAMAccountName").Value & "@CPEXCH.COM"
objPerson.Email = strEmail

' Create a message to initialize the mailbox
Set iMsg = CreateObject("CDO.Message")

with iMsg
.To = strEmail
.From = "administrator@minero.local"
.Subject = "Welcome to your new mailbox!"
.TextBody = "Note your new email address: " & strEmail
End With

Set iMsg = Nothing
End If
'Move to the next record
WScript.Echo intMailbox & " mailboxes were created."


Collapse -

Withouht Exchange?

by jtbjustin In reply to I haven't tested this wit ...

Thank you very much for the post.. This is very close to what I am looking for.. The only issue is this script is actually too good for my needs... I do not have an exchange server on my network, so I really just need to grab their username, and appened the and set it as their AD email.
Do you have any idea how I could fix this script? Or does anyone have another script?

Sorry my knowledge with VB is so limited.

Thank you!

Collapse -


by jtbjustin In reply to VB Script to set email in ...
Back to Networks Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums