Foreach issue trying to process data before its ready

Hi Chaps. Got an issue with a foreach

So I have a CSV file and lets say it just has two usernames. TESTUser1 and TestUser2. When the script is running its copying TestUser1 profile successfully and then it should move some folders based on that users copied data. What it looks to be doing is copying TestUser1 successfully and its then trying to move the data based on TestUser2 but that profile has not even copied yet.

Once its copied both profiles if i comment out Copy-Item -Recurse -Path U:$User $MigrationFolder -Force and run the script it moves the data successfully on both profiles. Am I missing a trick here with the foreach?

$MigrationFolder = "D:\Migration-Folder"
$MyPictures = "$User\Documents\My Pictures"
$Desktop = "$User\Redirected\Desktop"
$Downloads = "$User\Redirected\Downloads"
$DestinationFolder = "$User"

foreach($User in (Get-Content -Path D:\Users.csv)){

    #Copy CSV username H:\ to a migration folder
    Copy-Item -Recurse -Path U:\$User $MigrationFolder -Force
    
    #Move My Pictures to the root folder
    Move-Item -path $MigrationFolder'\'$MyPictures $MigrationFolder'\'$DestinationFolder -Force
    
    #Move Desktop to the root folder
    Move-Item -path $MigrationFolder'\'$Desktop $MigrationFolder'\'$DestinationFolder -Force
    
    #Move Downloads to the root folder
    Move-Item -path $MigrationFolder'\'$Downloads $MigrationFolder'\'$DestinationFolder -Force
  
}

You’re using the variable $User before it’s defined. :wink:

The way you build your paths inside the loop looks a little weird. I’d recommend to take a look at Join-Path.
Regardless of that I’d recommand to write your command a little move verbose. :wink:

Edit:
BTW: If your input file does not contain headers and more than one column you can name it *.txt as it is actually a plain text file. :wink:

OK thank you. Let me look into this further