Submitted By: Imran Hashim

Moves all the computers listed in a text file to a new OU. The script also creates and/or updates a log file with the original distinguished name of each computer.

Visual Basic
Edit|Remove
on error resume next

CONST ForReading = 1
Const ForAppending = 8

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTS = oFS.OpenTextFile("oldcomputers.txt",ForReading)
set oLS = oFS.OpenTextFile("movecomputerlog.txt",ForAppending,True)
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

'Modify the following line to desired OU
Set objNewOU = GetObject("LDAP://OU=Disabled Computers,dc=test,dc=local")
Do Until oTS.AtEndOfStream
        strComputer = oTS.ReadLine
        strQuery = _
          "<LDAP://" & strDNSDomain & ">;(&(objectCategory=computer)(cn=" & strComputer & "));adspath,cn;subtree"
        objCommand.CommandText = strQuery
        Set RS = objCommand.Execute
        While not RS.EOF
                strPath = RS.Fields("adspath")
                strName = "CN=" & RS.Fields("cn")
                oLS.Writeline(strPath)
                Set objMoveComputer = objNewOU.MoveHere(strPath,strName)
                RS.MoveNext
        Wend
Loop