Finding an OU
You want to find a specific OU within an Active Directory domain.
Using a graphical user interface
Using a command-line interface
> adfind default f "ou=<OU Name>"
Set objCommand = CreateObject("ADODB.Command") Set objConnection = CreateObject("ADODB.Connection") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" objCommand.ActiveConnection = objConnection strBase = "<LDAP://<DomainDN>>" strOUName = "Finance" strFilter = "(&(objectCategory=organizationalUnit)" _ "&(name=" & strOUName & "))" strAttributes = "distinguishedName" strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" objCommand.CommandText = strQuery objCommand.Properties("Page Size") = 100 objCommand.Properties("Timeout") = 30 objCommand.Properties("Cache Results") = False Set objRecordSet = objCommand.Execute While Not objRecordSet.EOF strName = objRecordSet.Fields("distinguishedName").Value Wscript.Echo "Distinguished Name: " & strName objRecordSet.MoveNext Wend objConnection.Close
In a heavily nested environment, you may need to locate an OU based on its name when you don't necessarily know its location. By using the ADUC GUI or a command-line tool with a search scope of subtree, you can easily recurse through the entire domain structure to find an OU based on its name, description, or any other attributes. In VBScript, you can use an ADO query to find objects that possess the specific attributes that you're looking for.
Recipe 5.2, Recipe 5.4, and MSDN: VBScript ADO Programming