VBScript – Create Active Directory Group

This article contains VBScript code to create group in Active Directory and it also contains  VBScript code to Create Bulk AD Groups from CSV file.
 

Summary

Create Active Directory Group by VB Script

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value for strgroupName if you want to give your own name for new group otherwise simply leave it.
3. Save the file with a .vbs extension, for example: CreateADGroup.vbs
4. Double-click the vb script file (or Run this file from command window) to create AD group.
    Note: You should run this VBScript on a machine with windows Active Directory domain.

Click to get VBScript code as file Download CreateADGroup.vbs

' CreateADGroup.vbs
' Sample VBScript to create a group in Active Directory .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------' 
Option Explicit
Dim strGroupName
Dim objRootLDAP,objContainer,objNewGroup
strGroupName = "MyTestGroup" 

Set objRootLDAP = GetObject("LDAP://rootDSE")
' You can give your own OU like LDAP://OU=TestOU instead of LDAP://CN=Users
Set objContainer = GetObject("LDAP://CN=Users," & _
objRootLDAP.Get("defaultNamingContext")) 

Set objNewGroup = objContainer.Create("Group", "cn=" & strGroupName)
objNewGroup.Put "sAMAccountName", strGroupName
objNewGroup.Put "Description", "AD Group created by VB Script"
objNewGroup.SetInfo

WScript.Echo "New Active Directory Group created successfully by using VB Script..."
WScript.Quit  

 

Create Bulk AD Groups from CSV File using VB Script

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: CreateBulkADGroupsFromCSVFile.vbs
3. Change the CSV file path C:NewGroups.csv with your own file path.
4. Change the domain name workdomain.local to your own domain name.
    Note:Your CSV file should contain group name as first column
5. Double-click vb script file (or Run this file from command window) to create Bulk Active Directory Groups from CSV file.

Click to get VBScript code as file Download CreateBulkADGroupsFromCSVFile.vbs

' CreateBulkADGroupsFromCSVFile.vbs
' Sample VBScript to create multiple AD Groups from CSV file .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------' 
Option Explicit  
' Variables needed for LDAP connection 
Dim objRootLDAP,objContainer 
' Variables needed for CSV File Information
Dim varFileName, objFSO, objFile
' Holding variables for group information import from CSV file 
Dim varGroupName, newGroupFields
Dim objNewGroup

Const ForReading = 1  
' Create a connection to the Active Directory Users container. 
Set objRootLDAP = GetObject("LDAP://rootDSE") 

' You can give your own OU like LDAP://OU=TestOU instead of LDAP://cn=Users
Set objContainer = GetObject("LDAP://cn=Users," & objRootLDAP.Get("defaultNamingContext")) 

' Specify the csv file full path.
varFileName = "C:\Newgroups.csv"

' Open the file for reading.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(varFileName, ForReading)
' Read the first line - csv columns -not needed for our proceess
objFile.ReadLine

' Skip the error while creating new group...(i.e- group already exists)
on error resume next
' Read the file and create new group.
Do Until objFile.AtEndOfStream
    ' Splits prioperty values.
    newGroupFields = Split(objFile.ReadLine,",")
   'First field as group name
    varGroupName = newGroupFields(0)

' Create new group
Set objNewGroup = objContainer.Create("Group","cn="&varGroupName)  
objNewGroup.put "sAMAccountName",lcase(varGroupName) 
objNewGroup.put "description","This group was created from csv file using vbscript"
objNewGroup.SetInfo 
Loop

WScript.Echo "Active Directory Groups created successfully from CSV file using VBScript."
WScript.Quit  

Advertisement

Leave a Comment