script to modify all users Homedirectory based on input excel spreadsheet with SAMaccountName

I have been given this script to exract all users with a specific homedirectory attribute (below)

I now need a script that will update this with \\newserver\users\%username% using the excel file created by below script.

csvde -d "DC=Domain,DC=local" -m -f c:\outputtest.csv -l name,sAMAccountName,homeDirectory -r "(&(objectCategory=user)(objectClass=user)(homeDirectory=\5c\5cServer\5cusers*)(cn=*)(displayName=*)(userPrincipalName=*))"

I have also been given a VB script that does the same thing so if either way can be edited to allow me to modify then that would be great



Dim ObjWb
Dim ObjExcel
Dim x, zz

strHomeDirectoryStart = "\\server1\users$"
Set objDomain = GetObject("LDAP://dc=northeast,dc=mydomain,dc=local")
Call ExcelSetup("sheet1") 
x = 1
Call enummembers(objDomain)
Sub enumMembers(objDomain)
	On Error Resume Next
	For Each objMember In objDomain 
		If ObjMember.Class = "user" Then 
			If Left(LCase(ObjMember.HomeDirectory), Len(strHomeDirectoryStart)) = LCase(strHomeDirectoryStart) Then
				x = x +1
				SamAccountName = ObjMember.samAccountName
				Cn = ObjMember.CN
				FirstName = objMember.GivenName
				LastName =
				initials = objMember.initials
				HomeDirectory = ObjMember.HomeDirectory
				HomeDrive = ObjMember.homeDrive
				objwb.Cells(x, 1).Value = objMember.Class
				objwb.Cells(x, 2).Value = SamAccountName
				objwb.Cells(x, 3).Value = CN
				objwb.Cells(x, 4).Value = FirstName
				objwb.Cells(x, 5).Value = LastName
				objwb.Cells(x, 6).Value = Initials
				objwb.Cells(x, 7).Value = HomeDirectory
				objwb.Cells(x, 8).Value = HomeDrive
				SamAccountName = "-"
				Cn = "-"
				FirstName = "-"
				LastName = "-"
				initials = "-"
				HomeDirectory = "-"
				HomeDrive = "-"
			End If
		End If

		If objMember.Class = "organizationalUnit" or objMember.Class = "container" Then
			enumMembers (objMember)
		End If
End Sub
Sub ExcelSetup(shtName)
Set objExcel = CreateObject("Excel.Application")
Set objwb = objExcel.Workbooks.Add
Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName)
Objwb.Name = "Active Directory Users" 
objExcel.Visible = True
objwb.Cells(1, 2).Value = "SamAccountName"
objwb.Cells(1, 3).Value = "CN"
objwb.Cells(1, 4).Value = "FirstName"
objwb.Cells(1, 5).Value = "LastName"
objwb.Cells(1, 6).Value = "Initials"
ObjWb.Cells(1, 22).Value = "HomeDirectory"
objwb.Cells(1, 23).Value = "HomeDrive"
End Sub
MsgBox "Active Directory Users Done !!!!" 

Toggle HighlightingOpen in New WindowSelect All

Open in new window

Who is Participating?
yo_beeDirector of Information TechnologyCommented:
Here is the LDIFDE:
ldifde  -d "DC=Domain,DC=local" -m -f c:\outputtest.ldf -l name,sAMAccountName,homeDirectory -r "(&(objectCategory=user)(objectClass=user)(homeDirectory=\5c\5cserver\5cfolder\*)(cn=*)(displayName=*)(userPrincipalName=*))"

Once exported locate the recently outputed file with Notepad.
You will need to do a Find & Replace for the Add value to Modityand Find & Replace for the home directory with the new one.

dn: CN=512425,OU=Misc Users,OU=All Users,DC=flhlaw,DC=local
changetype: add
homeDirectory: \\Server1\usersfolders\Users\512425
name: 512425
sAMAccountName: 512425

dn: CN=512425,OU=Misc Users,OU=All Users,DC=flhlaw,DC=local
changetype: Modify
homeDirectory: \\Server2\share1\Users\512425
name: 512425
sAMAccountName: 512425

Once done your will run

LDIFDE -i -f c:\outputtest.ldf -s <Domain Controller Name>  
Krzysztof PytkoSenior Active Directory EngineerCommented:
I don't know how to use VBScript for that but I could help you, using Quest PowerShell module for AD (if you're interested). Let me know if you want to do that using PS or wait for VBScript experts.

Quest PS is completely free and can be downloaded from

yo_beeDirector of Information TechnologyCommented:
You can also try LDIFDE export similar to csvde with the same command switches.
Once exported you can update the ldf file values from ADD to MODIFY.
Then run the import of the file.

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Premkumar YogeswaranAnalyst II - System AdministratorCommented:

This the tool i would use for Bulk AD modification, instead of going for script...

Hope it helps..

ncomperAuthor Commented:
Thanks all, i will post back with the solution that worked
yo_beeDirector of Information TechnologyCommented:
Just out of curiousity.  Why award points prior to confirming one if not all the solutions?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.