Bulk Move of Computer Objects within Active Directory

Posted on 2012-08-31
1 Ratings
Last Modified: 2013-03-26
I am looking for a vbscript that can read a txt file listing computer object names and then find them within other multiple OU's in Active Directory and move to a new OU.
Question by:seaninman
    1 Comment
    LVL 65

    Accepted Solution

    Hi, this script should do the job for you.  It will log the computer, old OU, and new OU.



    strInputFile = "Computers.txt"
    strOutputfile = "ComputerMoveResults.csv"
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strDNSDomain = objRootDSE.Get("defaultNamingContext")
    strNewOU = "LDAP://OU=TestOU," & strDNSDomain
    If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
    	Set objShell = CreateObject("WScript.Shell")
    	objShell.Run "cmd /k cscript """ & WScript.ScriptFullName & """", 1, False
    	Set objShell = Nothing
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Const intForReading = 1
    Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection
     ' Search entire Active Directory domain.
    strBase = "<LDAP://" & strDNSDomain & ">"
    strDetails = """Machine Name"",""Old Location"",""New Location"""
    While Not objInputFile.AtEndOfStream
    	strLine = objInputFile.ReadLine
    	strComputer = strLine
    	'strFilter = "(&(objectCategory=person)(objectClass=user))"
    	strFilter = "(&(objectClass=computer)(cn=" & strComputer & "))"
    	' Comma delimited list of attribute values to retrieve.
    	'strAttributes = "sAMAccountName,cn"
    	strAttributes = "cn,adsPath"
    	' Construct the LDAP syntax query.
    	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    	adoCommand.CommandText = strQuery
    	adoCommand.Properties("Page Size") = 100
    	adoCommand.Properties("Timeout") = 30
    	adoCommand.Properties("Cache Results") = False
    	' Run the query.
    	Set adoRecordset = adoCommand.Execute
    	' Enumerate the resulting recordset.
    	Do Until adoRecordset.EOF
    	    ' Retrieve values and display.
    		strComputerName = Replace(adoRecordset.Fields("cn").Value, "CN=", "")
    		WScript.Echo "Processing " & strComputer
    		strDetails = strDetails & VbCrLf & """" & strComputerName & """,""" & Replace(adoRecordset.Fields("AdsPath").Value, "CN=" & adoRecordset.Fields("cn").Value & ",", "") & """"
    	    ' Move the computer account
    		Set objOU = GetObject(strNewOU)
    		objOU.MoveHere adoRecordset.Fields("AdsPath").Value, vbNullString
    		strDetails = strDetails & ",""" & strNewOU & """"
    	    ' Move to the next record in the recordset.
    	' Clean up.
    	Set adoRecordset = Nothing
    Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
    objOutputFile.Write strDetails
    Set objOutputFile = Nothing
    WScript.Echo "Done"

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
    This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now