How can I use a script to add email addresses to a distribution group in Active Directory (AD)?

John Savill

July 1, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. The following script, which you can download here , will add email addresses to a distribution group. In the script, the first LDAP path is the location where the contacts will be created; you need to change it to a valid path for your environment. The second LDAP path is the distinguished name (DN) of the distribution group to which the contact will be added.

' Check whether all required arguments have been passed.If Wscript.Arguments.Count  required. For example:" & vbCrLf _& "cscript createcontact.vbs [email protected]"Wscript.Quit(0)End IfstrContactToAdd = Wscript.Arguments(0)Set containerObj = GetObject("LDAP://ou=Contacts,dc=dom,dc=com")If Err  0 Then WScript.Echo "Error " & Hex(Err)End If Err.ClearSet grpObj = GetObject("LDAP://cn=HRMailing,dc=dom,dc=com") If Err  0 Then WScript.Echo "Error " & Hex(Err)End If Err.ClearSet contactObj = containerObj.Create("contact","cn=" & strContactToAdd) If Err  0 Then WScript.Echo "Error " & Hex(Err)End If Err.ClearcontactObj.mail = strContactToAddcontactObj.mailNickName = strContactToAddcontactObj.SetInfo grpObj.Add(contactObj.AdsPath)grpObj.SetInfo

Run the script by using the following command:

D:Temp>cscript createcontact.vbs [email protected]

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like