[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Bulk Move of Computer Objects within Active Directory

Posted on 2012-08-31
Medium Priority
1 Endorsement
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

RobSampson earned 2000 total points
ID: 38359619
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


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

873 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