VBScript ; Create Active Directory Organisational Unit (OU)

VB Script Create Active Directory Organisational Unit (OU) – ADSI

Another useful time-saving tip when deploying a new Active Directory Tree.It is possible to script the creation of all Organisational Units in the Active Directory Tree using vbscript. This can save a great deal of time when it comes to the deployment of a new domain.

The following script will create a tree as follows: yourdomain.com > Sites – {new Top Level OU} > UK – {new sub-OU}

It will then create sub-OUs for each site listed in the object arrOus.

Site names must be seperated by a semi-colon (;)

For each Site sub-OU created a Users container and Computers container will be created.Again, the script is simple to modify for your environment.
 
‘—————————————–
Dim objRoot, objDomain, objOU, arrOUsDim strOUContainerDim intUser
 
Set oRoot = GetObject(“LDAP://rootDSE”)
oDomain = oRoot.Get(“defaultNamingContext”)
Set oDomain = GetObject(“LDAP://” & oDomain)strOUContainer =”OU=Sites”
Set objOU = objDomain.Create(“organizationalUnit”, strOUContainer)
objOU.SetInfostrOUContainer =”OU=UK,OU=Sites”
Set objOU = objDomain.Create(“organizationalUnit”, strOUContainer)
objOU.SetInfo
 
arrOUs = “Belfast;Birmingham;Bristol;Chessington;Dublin;Glasgow;Greenwich”arrOUs = Split(arrOUs,”;”)
 
For Each ou in arrOUs
        strOUContainer =”OU=” & ou & “,OU=UK,OU=Sites”
        Set objOU = oDomain.Create(“organizationalUnit”, strOUContainer) objOU.SetInfo strOUContainer =”OU=Users,OU=” & ou & “,OU=UK,OU=Sites” ‘
 
        ‘On Error Resume next
        Set objOU = oDomain.Create(“organizationalUnit”, strOUContainer)
        objOU.Put “Description”, “User Object Organisational Unit”
        objOU.SetInfo
        WScript.Echo “New OU created = ” & strOUContainer strOUContainer =”OU=Computers,OU=” & ou & “,OU=UK,OU=Sites” ‘
 
        Set objOU = oDomain.Create(“organizationalUnit”, strOUContainer)
        objOU.Put “Description”, “Computer Object Organisational Unit”
        objOU.SetInfo
        WScript.Echo “New OU created = ” & strOUContainer
Next’
‘—————————-

 The script has been tested on Windows Server 2000 and 2003 Domains.