Question

I have one list with 100 usernames formatted bloggsj and another list formatted joe.bloggs.

I have a folder full of profiles with each folder called e.g. \\server1\profiles\bloggsj

I need to robocopy all folders that match list1 to \\server2\profiles\joe.bloggs from list2.

I am expecting something like psudocode...

$list1 = currentnames.txt

$list2 = newnames.txt

$newpath = \\server2\profiles

$oldpath = \\server1\profiles

foreach item in $list 1
robocopy $oldpath + $list1.record to $newpath + list2.record
Was it helpful?

Solution

If you have a CSV with 2 columns, headers of OldName and NewName you could do this:

$NameMap = import-csv .\namemap.csv
gci \\server1\profiles|?{$_.PSIsContainer}|%{
    $source = $_.name
    $dest = ($NameMap|?{$_.newname -match $source}|select -expandproperty oldname)
    $_.MoveTo("\\Server2\Profiles\$dest")
}

That will load a CSV, pull a folder listing of the old servers profiles, and for each one move it to the new server with the new profile name.

Without the CSV it gets a lot more complicated, and prone to errors because we have to make assumptions. You could do it manually I suppose, assuming that all old names are last name followed by first initial and the new name is first name (dot) last name. This will load the lists, iterate through the new names and create a hashtable associating the new names to the old names (based on the previous assumptions). Then it pulls a folder listing of the old profiles and moves them to the new server with the new profile name provided by the hashtable.

$NameMap = @{}
$OldNames = gc oldnames.txt
$NewNames = gc newnames.txt
ForEach($Name in $NewNames){
    $FindIt = "$($Name.Substring($Name.Length-1,1)).*?\.$($Name.Substring(0,$Name.Length-1))"
    $NewName = $NewNames|?{$_ -match $FindIt}
    $NameMap.Add($NewName,$Name)
}
GCI "\\Server1\Profiles"|?{$_.PSIsContainer}|%{$Dest = $NameMap[$_.Name];$_.MoveTo("\\Server1\Profiles\$Dest")}

Edit: Ok, if you really feel that you need to use RoboCopy, once you have the mapping CSV loaded you can do:

$NameMap|%{Robocopy "\\server1\profiles\$($_.oldname)","\\server2\profiles\$($_.newname)","/E","/COPYALL"}

Or if you use the Make-Your-Own_Hashtable method you could do something like:

GCI "\\Server1\Profiles"|?{$_.PSIsContainer}|%{$Dest = $NameMap[$_.Name];RoboCopy $_.FullName,"\\Server1\Profiles\$Dest","/E","/COPYALL"}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top