We help IT Professionals succeed at work.

VB script to transfer user files between domains

We are about to kick off a big project moving about 200 users between domains, the users will be moved onto a new domain, with a new format of username and a new computer.

I want to use a VB script to move, Desktop & Favorites & My Documents from within Documents and Settings.

Ideally, one vb script on the old computer to be run when logged onto a admin, that will prompt for the username, then move the three above folders to a network share, then prompt when finished.

Then the user logs onto the new computer and off, so that a admin can log on and...

Then another vb script to prompt for the old username and the new username, and then move the three folders back onto the NEW computer and prompt when complete...

I have a little bit of code before, but am not a code guru... please help!
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
 
 
RootSource = "D:\Audio\"
RootDestination = "\\some computer name\audio\"
 
strfolder = InputBox("Please Input folder name:", "Your vbs")
 
If not objFSO.FolderExists(RootDestination & strfolder) Then
 
	objFSO.CreateFolder(RootDestination & strfolder)
End IF 
 
strcmd = "xcopy """ & RootSource & strfolder & "\*.*"" """ & RootDestination & strfolder & """ /S /Y /E "
'Wscript.Echo strcmd
 
strCommand = WshShell.Run(strcmd, 0, True) 
 
Wscript.Echo strCommand 
 
Set objFSO = nothing
Set WshShell = nothing

Open in new window

Comment
Watch Question

Hi

Follows 2 scripts the first one is to backup users profile and the sencond to restore them. Save each script on different VBS files.

I also added a few checks.

'==================================================================
'							BACK UP SCRIPT
'==================================================================
On Error Resume Next

Const OverWriteFiles = True

strTargetShare = "SHARE PATH" ' **** The path needs to finish with a back slash = \

strusername = InputBox("Please Input username name:", "User Profile Backup")

If strusername = "" Then
	box = MsgBox ("Blank Username. Please restart the script",16, "User Profile Backup")
	WScript.Quit
End If

strProfileFolder = "C:\Documents and Settings\" & strusername

Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strProfileFolder) Then

   box = MsgBox ("Source Profile Folder Not Found. Please check username.",16, "User Profile Backup")
   WScript.Quit
End If

strTargetFolder = strTargetShare & strusername & "\"

If objFSO.FolderExists(strTargetFolder) Then

   box = MsgBox ("Target Folder already exists. Would you like to overwrite?",4132, "User Profile Backup")

	If Box <> 6 Then
		box = MsgBox ("Script Stopped",64, "User Profile Backup")
		WScript.Quit
	End If
Else
	objFSO.CreateFolder (strTargetFolder)
End If

objFSO.CopyFolder strProfileFolder & "\Favorites" , strTargetFolder & "\Favorites" , OverWriteFiles
objFSO.CopyFolder strProfileFolder & "\Desktop" , strTargetFolder & "\Desktop"  , OverWriteFiles
objFSO.CopyFolder strProfileFolder & "\My documents" , strTargetFolder & "\My documents"  , OverWriteFiles

WScript.Quit

'==================================================================
'							END OF BACK UP SCRIPT
'==================================================================


'==================================================================
'							RESTORE SCRIPT
'==================================================================

On Error Resume Next

Const OverWriteFiles = True

strSourceShare = "SHARE PATH" ' The path needs to finish with \

strusername = InputBox("Please Input username name:", "User Profile Restore")

If strusername = "" Then
	box = MsgBox ("Blank Username. Please restart the script",16, "User Profile Restore")
	WScript.Quit
End If

strProfileFolder = "C:\Documents and Settings\" & strusername

Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strProfileFolder) Then

   box = MsgBox ("Target Profile Folder Not Found. Please check username.",16, "User Profile Restore")
   WScript.Quit
End If

strSourceFolder = strSourceShare & strusername & "\"

If Not objFSO.FolderExists(strSourceFolder) Then

   box = MsgBox ("Source Folder Not Found. Please check username and Share.",16, "User Profile Restore")
   WScript.Quit

End If

objFSO.CopyFolder strSourceFolder & "\Favorites" , strProfileFolder & "\Favorites" , OverWriteFiles
objFSO.CopyFolder strSourceFolder & "\Desktop" , strProfileFolder & "\Desktop"  , OverWriteFiles
objFSO.CopyFolder strSourceFolder & "\My documents" , strProfileFolder & "\My documents"  , OverWriteFiles


WScript.Quit


'==================================================================
'							END RESTORE SCRIPT
'==================================================================

Open in new window

Are you looking to just move regular user Files between machines?  That's all that will happen if you copy those folders.  Nothing user-related in the registry will be moved at all such as application settings , Windows preferences, etc.  That might not be a bad thing to start from scratch though!

If you are wanting to move actual user profiles, Microsoft has a nice tool called User State Migration Tool (USMT) that will do this and you can even migrate profiles between operating system versions if the users are going from XP to Vista or 7 on their new computers.

If they are going from XP to Vista or 7 and you just want to use MaxSoullard's script, the User Profile folder path is also going to be different on the different operating systems.  I use this little bit of code if my scripts need direct access to non-specific User Profile folders.  Replace lines 17 and 68 in MaxSoullard's code above with this attached code:

It will grab the "AllUsersProfile" variable from the local machine and remove the "All Users" part at the end then add the User name from the Input box.

Set objWSHShell = WScript.CreateObject("WScript.Shell")
strProfileFolder = Replace(objWSHShell.ExpandEnvironmentStrings("%AllUsersProfile%"),"All Users","") & strUserName

Open in new window

Author

Commented:
Yeah I've usmt before, but my personal preference is a clean copy of just the files that the user needs, plus with the users going onto a different domain etc.

Thank you for all your help.