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

Posted on 2011-10-13
Last Modified: 2012-05-12
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

Question by:ncomper
    LVL 39

    Assisted Solution

    by:Krzysztof Pytko
    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

    LVL 21

    Expert Comment

    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.

    LVL 17

    Assisted Solution

    by:Premkumar Yogeswaran

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

    Hope it helps..

    LVL 21

    Accepted Solution

    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>  
    LVL 5

    Author Closing Comment

    Thanks all, i will post back with the solution that worked
    LVL 21

    Expert Comment

    Just out of curiousity.  Why award points prior to confirming one if not all the solutions?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    Exit Loop after User Input 4 24
    What is UDP port 389 used for? 5 44
    Exchange 2007 13 17
    Security Permissions Issues 10 22
    This is my first article in EE and english is not my mother tongue so any comments you have or any corrections you would like to make, please feel free to speak up :) For those of you working with AD, you already are very familiar with the classi…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

    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

    8 Experts available now in Live!

    Get 1:1 Help Now