Let’s say as part of a migration you are changing login IDs for everyone but you want to continue to use the %USERNAME% variable to map their home drives, etc. Here’s an easy way to rename the existing home directories without losing rights assigned to them.
The input file is a CSV file with the old and new sAMAccountNames. Change the server and login info below. This is assuming you’re connecting from a different domain and want to use a Domain Admin account that has rights to the file server that the home directories are on. You could exclude the login info and just map a drive if you are already logged in with the appropriate permissions.
Option Explicit
On Error Resume Next
Dim arrTxtArray()
Dim oldnewDir
Dim SearchString
Dim objTextFile
Dim strNextLine
Dim intSize
Dim objFSO1
Dim objFSO2
Dim wshNet
Dim i
Dim newArray
intSize = 0
oldnewDir = "E:migrationhomedirs.csv"
SearchString = ","
Const ForReading = 1
Set wshNet = CreateObject("WScript.Network")
wshNet.MapNetworkDrive "S:","\serverusers",False,"domainusername","password"
If objFSO.FolderExists("S:") Then
WScript.Echo("S: drive mapped successfully.")
WScript.Echo()
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO1.OpenTextFile(oldnewDir, ForReading)
Do Until objTextFile.AtEndofStream
strNextLine = objTextFile.ReadLine
If InStr (strNextLine, SearchString) Then
ReDim Preserve arrTxtArray(intSize)
arrTxtArray(intSize) = strNextLine
intSize = intSize +1
End If
Loop
objTextFile.Close
For i = LBound(arrTxtArray) To UBound(arrTxtArray)
If InStr (arrTxtArray(i), ",") Then
newArray = Split (arrTxtArray(i),",")
‘WScript.Echo "Old Directory Name: " & newArray(0)
‘WScript.Echo "New Directory Name: " & newArray(1)
‘WScript.Echo(" ")
End If
‘Check to make sure old directory exists and change the name
If objFSO.FolderExists("S:"& newArray(0)) Then
WScript.Echo "Old Directory Name Exists:" & newArray(0)
objFSO2.moveFolder "S:" & newArray(0),"S:" & newArray(1)
‘Check to make sure new directory exists
If objFSO.FolderExists("S:"& newArray(1)) Then
WScript.Echo "Home Directory Name Change Successful For: " & newArray(1)
WScript.Echo(" ")
Else
WScript.Echo "Home Directory Name Change Unsuccessful For: " & newArray(1)
WScript.Echo(" ")
End If
Else
WScript.Echo "Old Directory Name Does Not Exist:" & newArray(0)
WScript.Echo(" ")
End If
Next
WScript.Echo("All done!")
WScript.Echo(" ")
Else
WScript.Echo("S: drive did not map correctly. Check permissions or login ID and try again.")
End If