Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.
strOutputfile = "Users_HomeDirectoryChange.csv"
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
WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
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.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strDetails = """User Name"",""Full Name"",""AdsPath"",""Old Home Directory"",""New Home Directory"""
strOldHomeDirectory = "\\fileserver01\users\"
strNewHomeDirectory = "\\fileserver02\users\"
strFilter = "(&(objectCategory=person)(objectClass=user)(homeDirectory=" & Replace(strOldHomeDirectory, "\", "\5c") & "*))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "samAccountName,cn,adsPath,homeDirectory"
' 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.
strUserName = adoRecordset.Fields("samAccountName").Value
If strUserName = "rsampson" Then
strFullName = adoRecordset.Fields("cn").Value
strOUPath = Replace(adoRecordset.Fields("adsPath").Value, "LDAP://CN=" & strFullName & ",", "")
strOldHomeDirectory = adoRecordset.Fields("homeDirectory").Value
WScript.Echo "Processing " & strFullName
Set objUser = GetObject(adoRecordset.Fields("adsPath").Value)
objUser.homeDirectory = strNewHomeDirectory & strUserName
objUser.SetInfo
Set objUser = Nothing
strDetails = strDetails & VbCrLf &_
"""" & strUserName & """" &_
",""" & strFullName & """" &_
",""" & strOUPath & """" &_
",""" & strOldHomeDirectory & """" &_
",""" & strNewHomeDirectory & strUserName & """"
End If
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
Set adoRecordset = Nothing
adoConnection.Close
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strDetails
objOutputFile.Close
Set objOutputFile = Nothing
WScript.Echo "Done. Please see " & strOutputFile
MsgBox "Done. Please see " & strOutputFile
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.
you can actually just bulk select the users in Active Directory users and computers and then right click properties.
You can enter the variable in there and it will update all users.
Alternatively you can use ADModify: http://admodify.codeplex.com/