Steve Bona
asked on
Copy files from old server to new server with Robocopy
I need to copy the old user directories to their new directory they are already using. In order not to mix the directories, I created a new sub-directory in each of the users' directories before making the copy.
So i write a little powershell script
$Importcsv = "C:\csv\MoveHomerDir.csv"
$HomeDirList = Import-csv $Importcsv
Foreach($HomeFolder in $HomeDirList){
$NewDir = "Migrate_Directory"
$CurrentDirPath = $HomeFolder.NewHomeDir
$NewDirPath = "$CurrentDirPath\$NewDir"
#Verify if new subfolder allready exist
If (Test-Path -Path $NewDirPath){
Write-host "Directory $NewDir allready exist in $CurrentDirPath" -ForegroundColor Yellow
}
Else {
New-Item -Name $NewDir -Path $CurrentDirPath -ItemType Directory
}
#Start copy Directory
Foreach ($HomeDir in $HomeDirList) {
$HomeDirSource = $HomeDir.OldHomeDir
$HomeDirDest = $HomeDir.NewHomeDir
robocopy "$HomeDirSource" "$HomeDirDest\$NewDir" /MIR /COPY:DATOU /LOG+:C:\Logs\TestMigrateH omeDirecto ry.log
}
}
My script works but copy directory seems to repeat itself, i don't understand why.
Did i forget something
So i write a little powershell script
$Importcsv = "C:\csv\MoveHomerDir.csv"
$HomeDirList = Import-csv $Importcsv
Foreach($HomeFolder in $HomeDirList){
$NewDir = "Migrate_Directory"
$CurrentDirPath = $HomeFolder.NewHomeDir
$NewDirPath = "$CurrentDirPath\$NewDir"
#Verify if new subfolder allready exist
If (Test-Path -Path $NewDirPath){
Write-host "Directory $NewDir allready exist in $CurrentDirPath" -ForegroundColor Yellow
}
Else {
New-Item -Name $NewDir -Path $CurrentDirPath -ItemType Directory
}
#Start copy Directory
Foreach ($HomeDir in $HomeDirList) {
$HomeDirSource = $HomeDir.OldHomeDir
$HomeDirDest = $HomeDir.NewHomeDir
robocopy "$HomeDirSource" "$HomeDirDest\$NewDir" /MIR /COPY:DATOU /LOG+:C:\Logs\TestMigrateH
}
}
My script works but copy directory seems to repeat itself, i don't understand why.
Did i forget something
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i did not know that robocopy will create the target directory automatically.