Script to find and change users' with homedirectory path on one server to another server AD

I need a script to find users in AD who have their homedirectory on a specific server, and then change that homedirectory to a new path.

E.g., I have many users who currently are using the path \\servername\username$.  We're moving the folders to a new server, and I only want to use one share.  So for those users, I'd like to find them and change the path to \\newservername\users$\%username%.

If I have to, I can do it through ADUC by selecting them and modifying them, but I'd rather script it.
I've already looked through a lot of scripting guys scripts, and can't seem to put the two together (Find and then replace).
LVL 15
zelron22Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CBaldersonConnect With a Mentor Commented:
Ok, now you are getting complicated!  :-)
Lets try to set up a loop using an array... paste the snippet below in you Excel VB Editor
Sub GetHR_Data()
'Add as many OU's as you need to query
strOU = Array("OU=Users,OU=PARENT1,", "OU=Users,OU=PARENT2,", "OU=Users,OU=PARENT3,")
strSheetName = "HR"
 
'Select Sheet to write data to
Sheets(strSheetName).Select
 
'Clear Data
Columns("A:A").Select
Selection.ClearContents
Range("A1").Select
 
'Call AD for as many arguments that exist in the strOU list/array
ArrLength = UBound(strOU) + 1
For i = 1 To ArrLength
Row = Sheets(strSheetName).UsedRange.Rows.count
Debug.Print usedRowsCount
result = GetAD_Info(strOU(i - 1), Row)
Next i
 
End Sub
Function GetAD_Info(x, y)
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
 
strContainer = x & strDNSDomain
Row = y
 
Set objOU = GetObject("LDAP://" & strContainer)
 
'Get AD Data and write to sheet
For Each objUser In objOU
    If objUser.class = "user" Then
        'Debug.Print objUser.cn
        Cells(Row, 1) = objUser.cn
        Row = Row + 1
    End If
Next
 
End Function

Open in new window

0
 
CBaldersonCommented:
Hi, give this a try.

Save as a .VBS file and Test first
Change the values based on your environment.  


 
' --------------------------------------------------------------' 
' UserHomeDir .vbs
' Change user's HomeFolder in a named OU (find and replace)
' May 2009
' --------------------------------------------------------------' 
Option Explicit
Dim objOU, objUser, objRootDSE
Dim strContainer, strDNSDomain
Dim strOldPath, strNewPath, strUser, strhomeDirectory
Dim count
 
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
 
' -------------------------------------------------------------'
' Important change OU= to reflect your domain
' -------------------------------------------------------------'
strContainer = "OU=Users,OU=Test,"
strOldPath = "\\test\Users\"
strNewPath = "\\NewServer\Users\"
strContainer = strContainer & strDNSDomain
 
' Loop through OU=, Find Users that Match
set objOU =GetObject("LDAP://" & strContainer )
count=0
For each objUser in objOU
If objUser.class="user" then
	If INSTR(UCASE(objUser.homeDirectory),UCASE(strOldPath)) then
		'wscript.echo objUser.name
		ChangeHomeDir()
		count = count +1
	End If
End If
Next 
 
wscript.echo "Processed Users = " & count
 
Function ChangeHomeDir()
'Change User homeDirectory to strNewPath
	strUser = objUser.sAMAccountName
	strHomeDirectory = strNewPath & strUser
	objUser.homeDirectory = strHomeDirectory
	objUser.SetInfo
End Function

Open in new window

0
 
zelron22Author Commented:
This is great, but I would like to find them by the homedirectory they already have, vs. an OU that they're in.
0
 
CBaldersonCommented:
The OU is a filter to speed things up and to help you isolate a few targets before modifying the entire domain.  
The script does a find and replace based on the homeDirectory value.

You should be able to remove the line
             strContainer = "OU=Users,OU=Test,"
The result would be a query of you entire domain for homeDirectory value match strOldPath and replace them with the strNewPath.

Hope that helps
0
 
CBaldersonCommented:
Hold on a sec, your old path is \\servername\username$ we can make a few quick modifications to sort that.  I don't have a test for this version but it should do the trick - give it a test run.

 
' --------------------------------------------------------------' 
' UserHomeDir .vbs
' Change user's HomeFolder in a named OU (find and replace)
' May 2009
' --------------------------------------------------------------' 
Option Explicit
Dim objOU, objUser, objRootDSE
Dim strContainer, strDNSDomain
Dim strOldPath, strNewPath, strUser, strhomeDirectory
Dim count
 
' Bind to Active Directory Domain
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
 
' -------------------------------------------------------------'
' Important change OU= to reflect your domain
' -------------------------------------------------------------'
strContainer = "OU=Users,OU=Brussels,"
strOldPath = "\\serversname\"
strNewPath = "\\servername\Users$\"
strContainer = strContainer & strDNSDomain
 
' Loop through OU=, Find Users that Match
set objOU =GetObject("LDAP://" & strContainer )
count=0
For each objUser in objOU
If objUser.class="user" then
	If UCASE(objUser.homeDirectory)=UCASE(strOldPath & objUser.sAMAccountName & "$")) then
		'wscript.echo objUser.name
		ChangeHomeDir()
		count = count +1
	End If
End If
Next 
 
wscript.echo "Processed Users = " & count
 
Function ChangeHomeDir()
'Change User homeDirectory to strNewPath
	strUser = objUser.sAMAccountName
	strHomeDirectory = strNewPath & strUser
	objUser.homeDirectory = strHomeDirectory
	objUser.SetInfo
End Function

Open in new window

0
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.