We help IT Professionals succeed at work.

Copy files from old server to new server with Robocopy

Steve Bona
Steve Bona asked
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\TestMigrateHomeDirectory.log
}

}

My script works but copy directory seems to repeat itself, i don't understand why.
Did i forget something

CopyHomeDirectoryCSVFileRobocopyLog
Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
The error is rather obvious once you reformat your script with proper indentation (see, for example, https://en.wikipedia.org/wiki/Indentation_style)
You have two nested ForEach loops (lines 4 and 15), so that the inner loop (which currently does the actual robocopy) is started 6 times:
$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\TestMigrateHomeDirectory.log
	}
}

Open in new window


robocopy will create the target directory automatically, no need to do that manually.
NewDir is static, so it should be set outside the ForEach loop.
Try it like this:
$HomeDirList = Import-Csv -Path "C:\csv\MoveHomerDir.csv"
$NewDir = "Migrate_Directory"
ForEach ($HomeDir in $HomeDirList) {
	#Start copy Directory
	& robocopy.exe "`"$($HomeDir.OldHomeDir)`"" "`"$($HomeDir.NewHomeDir)\$($NewDir)`"" /MIR /COPY:DATOU /LOG+:C:\Logs\TestMigrateHomeDirectory.log
}

Open in new window

Steve BonaInformation Technology Specialist

Author

Commented:
Thanks,
i did not know that robocopy will create the target directory automatically.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.