Zachary Loeber

The personal website of Zachary Loeber.

Exchange: Remove entire OU from address book

Here is another script that I hacked together in part of an AD/Exchange cleanup task to remove disabled users from the address book. This script, more specifically, removes an entire OU of users from the address book, so make sure that all the users are disabled in the OU you will be running this against! 🙂

Save as a VBS file and run from your exchange server:

On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://OU=Some 2nd level OU,OU=Some Top level OU,dc=corp,dc=contoso,dc=com' WHERE objectClass='User'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strContactPath = objRecordSet.Fields("ADsPath").Value
MsgBox (strContactPath)
Set objContact = GetObject(strContactPath)
objContact.MSExchHideFromAddressLists = TRUE
objContact.SetInfo
objRecordSet.MoveNext
Loop
comments powered by Disqus