Link to home
Start Free TrialLog in
Avatar of waltforbes
waltforbesFlag for Bahamas

asked on

How do I script bulk creation of AD groups?

Points of My Scenario:
1. I am admin of a Windows Server 2003 domain
2. I need to create 96 (ninety-six) domain local groups
3. I have a text file containing the group names - one group per line
QUESTION: How can I script the creation of these 96 groups into an OU, using the text file?
SOLUTION
Avatar of mariofxp
mariofxp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobSampson
Hi, you can try this.

Regards,

Rob.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")

' OU to create groups in
strOU = "CN=Users," & strDomain
' File to read group names from
strGroupsFile = "groups.txt"
' File to record results to
strResultsFile = "results.txt"

Const ADS_GROUP_TYPE_LOCAL_GROUP = &h4
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objResults = objFSO.CreateTextFile(strResultsFile, True)
objResults.WriteLine "Creating Groups " & Now
Set objGroups = objFSO.OpenTextFile(strGroupsFile, intForReading, False)
While Not objGroups.AtEndOfStream
	strGroupName = objGroups.ReadLine
	Set objGroup = objOU.Create("Group", "cn=" & strGroupName)
	objGroup.Put "sAMAccountName", strGroupName
	objGroup.Put "groupType", ADS_GROUP_TYPE_LOCAL_GROUP
	On Error Resume Next
	objGroup.SetInfo
	If Err.Number <> 0 Then
		objResults.WriteLine strGroupName & " already exists."
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0	
		objResults.WriteLine strGroupName & " created."
	End If
	Err.Clear
	On Error GoTo 0
Wend
objGroups.Close
objResults.Close

WScript.Echo "Done. Please see " & strResultsFile

Open in new window

Hi, if you wish ti try other methods, like Quest PowerShell module for AD or Microsoft DS Tools, please let me know. I would help you then. If not, follow with Rob's syntax :)

Regards,
Krzysztof
Avatar of waltforbes

ASKER

To Rob: please note the following error I receive:
C:\Scripts\Create_Groups_from_File.vbs(19, 2) Microsoft VBScript runtime error:
Object required: 'objOU'

To iSiek: I am interesed in Quest PowerShell module for AD.
OK, so try this syntax

Get-Content c:\groups.txt | %{ New-QADGroup -name "$_" -ParentContainer "ou=GroupLocation,dc=domain,dc=local" -DisplayName "$_" -GroupType "Security" -GroupScope "DomainLocal" -SamAccountName "$_"

Krzysztof
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm so happy!