Link to home
Start Free TrialLog in
Avatar of zelron22
zelron22

asked on

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).
Avatar of CBalderson
CBalderson

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

Avatar of zelron22

ASKER

This is great, but I would like to find them by the homedirectory they already have, vs. an OU that they're in.
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
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

ASKER CERTIFIED SOLUTION
Avatar of CBalderson
CBalderson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial