Script that will add multiple accounts to a AD group

Posted on 2012-09-14
Last Modified: 2012-09-24
Does anyone have a simple script that will add multiple (300+) accounts to an ad group?  I need this asap!

I appreciate it.
Question by:mystikal1000
    LVL 23

    Expert Comment

    by:Stelian Stan
    LVL 23

    Expert Comment

    by:Stelian Stan
    With some tweaking this should work:

    Author Comment

    I rather have a vbs script than powershell.  :)
    LVL 23

    Expert Comment

    by:Stelian Stan
    LVL 65

    Accepted Solution

    Hi, see if this VBScript will do the job for you.  I haven't tested it yet.  Add usernames to users.txt and specify your group in the Set objGroup line, and see how it goes.



    Set objGroup = GetObject("CN=GroupName,OU=GroupOU,DC=domain,DC=com")
    strUsersFile = "users.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Const intForReading = 1
    Set objUsers = objFSO.OpenTextFile(strUsersFile, intForReading, False)
    strAllDNs = ""
    While Not objUsers.AtEndOfStream
    	strUserName = objUsers.ReadLine
    	strUserDN = ""
    	strUserDN = Get_LDAP_User_Properties("user", "samAccountName", strUserName, "distinguishedName")
    	If strUserDN <> "" Then
    		If strAllDNs = "" Then
    			strAllDNs = strUserDN
    			strAllDNs = strAllDNs & "|" & strUserDN
    		End If
    		WScript.Echo "Could not find " & strUserName & ". User will not be added to new groups."
    	End If
    arrAllDNs = Split(strAllDNs, "|")
    On Error GoTo 0	
    objGroup.PutEx ADS_PROPERTY_APPEND, "member", arrAllDNs 
    If Err.Number = 0 Then
    	WScript.Echo "Users have been added."
    	WScript.Echo "Error " & Err.Number & ": " & Err.Description
    End If
    Set objGroup = Nothing
    MsgBox "Done"
    Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
    	If InStr(strObjectToGet, "\") > 0 Then
    		arrGroupBits = Split(strObjectToGet, "\")
    		strDC = arrGroupBits(0)
    		strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
    		strObjectToGet = arrGroupBits(1)
    		Set objRootDSE = GetObject("LDAP://RootDSE")
    		strDNSDomain = objRootDSE.Get("defaultNamingContext")
    	End If
    	strBase = "<LDAP://" & strDNSDomain & ">"
    	' Setup ADO objects.
    	Set adoCommand = CreateObject("ADODB.Command")
    	Set adoConnection = CreateObject("ADODB.Connection")
    	adoConnection.Provider = "ADsDSOObject"
    	adoConnection.Open "Active Directory Provider"
    	adoCommand.ActiveConnection = adoConnection
    	' Filter on user objects.
    	'strFilter = "(&(objectCategory=person)(objectClass=user))"
    	strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"
    	' Comma delimited list of attribute values to retrieve.
    	strAttributes = strCommaDelimProps
    	arrProperties = Split(strCommaDelimProps, ",")
    	' Construct the LDAP syntax query.
    	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    	adoCommand.CommandText = strQuery
    	adoCommand.Properties("Page Size") = 100
    	adoCommand.Properties("Timeout") = 30
    	adoCommand.Properties("Cache Results") = False
    	WScript.Echo ""
    	WScript.Echo "Executing " & strQuery
    	' Run the query.
    	Set adoRecordset = adoCommand.Execute
    	' Enumerate the resulting recordset.
    	Do Until adoRecordset.EOF
    	    ' Retrieve values and display.    
    	    For intCount = LBound(arrProperties) To UBound(arrProperties)
    	    	If strDetails = "" Then
    	    		strDetails = adoRecordset.Fields(intCount).Value
    	    		strDetails = strDetails & VbCrLf & adoRecordset.Fields(intCount).Value
    	    	End If
    	    ' Move to the next record in the recordset.
    	' Clean up.
    	Get_LDAP_User_Properties = strDetails
    End Function 

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now