Migrate home directories using Powershell
-
Monday, February 21, 2011 4:10 AM
We are migrating to a new forest and I need to migrate users home directories over to the new forest. Problem is the naming convention on the forest is different so the user id has changed. I want to use powershell or robocopy (or combination of both) to do the copy but pull the source and destination from a txt file.
So basically copy:\\fileshare\userid1 to \\fileshare\iduser1
\\fileshare\userid2 to \\fileshare\iduser2
\\fileshare\userid3 to \\fileshare\iduser3Something like:
Robocopy source= olduserid.txt dest=newuserid.txt and it runs the robocopy process for each line in the txt files
I have got as far as pulling the source or dest directory using the following powershell command but can't figure how i can specify both. Any ideas?
gc d:\olduserid.txt | % { robocopy \\fileshare\olduserid\$_ \\fileshare\newuserid\ }
Sorry new with poweshell.
Cheers
All Replies
-
Monday, February 21, 2011 7:26 AM
Make csv file with columns source and dest
source,dest
\\fileshare\userid1,\\fileshare\iduser1In powershell you may import it and use Copy-Item:
$shares = Import-Csv p:\test.csv
foreach ($sh in $shares) { Copy-Item $sh.source $sh.dest -recurse}- Proposed As Answer by MichalGajdaMVP Wednesday, February 23, 2011 7:39 PM
-
Monday, February 21, 2011 9:43 PM
Hi jstirl
I wrote a script to do just that last week. First I created a spread sheet with the Users Names for example:
new
class
old
00BP901
PUPIL
00BP11
00BU900
PUPIL
00BU11
00EP903
PUPIL
00EP11
00GA900
PUPIL
00ga11
00JK903
PUPIL
00JK11
00JM904
PUPIL
00jm11
00LP902
PUPIL
00lp11
I then saved this to C:\Userlist.csv. I then used the following Script to create a simple RoboCopy batch file:
$siteCode = 'SITEA'
$Users = Import-Csv C:\Userlist.csv
# Create RoboCopy batch file for User Data Migration
$robocopy = foreach ( $user in $users )
{
switch ( $user . Class )
{
Staff { "robocopy E:\Users\$($user.Old) P:\Users\Staff\$($user.New) /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
Admin { "robocopy E:\Users\$($user.Old) P:\Users\Staff\$($user.New) /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
pupil { "robocopy E:\Users\$($user.Old) P:\Users\Pupils\$($user.New) /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
Staff { "robocopy E:\NTProfiles\$($user.Old)\Favorites\ P:\Users\Staff\$($user.New)\Favorites\ /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
Admin { "robocopy E:\NTProfiles\$($user.Old)\Favorites\ P:\Users\Staff\$($user.New)\Favorites\ /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
pupil { "robocopy E:\NTProfiles\$($user.Old)\Favorites\ P:\Users\Pupils\$($user.New)\Favorites\ /mir /r:1 /w:2 >> $($SiteCode)UserData.log" }
}
}
$robocopy | Out-File "C:\RoboCopy$($SiteCode)UserData.bat" -Encoding ascii
The output looked like this once complete:
robocopy E:\Users\00BP11 P:\Users\Pupils\00BP901 /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\NTProfiles\00BP11\Favorites\ P:\Users\Pupils\00BP901\Favorites\ /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\Users\00BU11 P:\Users\Pupils\00BU900 /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\NTProfiles\00BU11\Favorites\ P:\Users\Pupils\00BU900\Favorites\ /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\Users\00EP11 P:\Users\Pupils\00EP903 /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\NTProfiles\00EP11\Favorites\ P:\Users\Pupils\00EP903\Favorites\ /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\Users\00ga11 P:\Users\Pupils\00GA900 /mir /r:1 /w:2 >> SITEAUserData.log
robocopy E:\NTProfiles\00ga11\Favorites\ P:\Users\Pupils\00GA900\Favorites\ /mir /r:1 /w:2 >> SITEAUserData.log
Obviously you would need to change the paths and possibly some of the switches for RoboCopy but this did the job for me at one of my clients and should point you in the right direction.
John Milner | MCITP: SA/EMA | MCTS | MCSE: S | MCSA: S/M | My Blog | This post is provided "AS IS" with no warranties or guarantees, and confers no rights.- Proposed As Answer by jfrmilner Wednesday, February 23, 2011 7:10 PM
- Marked As Answer by Dale QiaoModerator Friday, February 25, 2011 1:32 AM

