# Foreach issue trying to process data before its ready

**URL:** <https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669>\
**Category:** PowerShell Help\
**Created:** [June 21, 2022, 9:42am UTC](https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669 "2022-06-21T09:42:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mctech](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@mctech](https://forums.powershell.org/u/mctech)\
**Post date:** [June 21, 2022, 9:42am UTC](https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669/1 "2022-06-21T09:42:40Z")

</div>

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?

```auto
$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
  
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 21, 2022, 9:48am UTC](https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669/2 "2022-06-21T09:48:47Z")

</div>

You’re using the variable `$User` before it’s defined. 😉

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. 😉

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. 😉

---

<div class="post-metadata">

**Author:** ![mctech](https://avatars.discourse-cdn.com/v4/letter/m/41988e/32.png) [@mctech](https://forums.powershell.org/u/mctech)\
**Post date:** [June 21, 2022, 10:08am UTC](https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669/3 "2022-06-21T10:08:53Z")

</div>

OK thank you. Let me look into this further

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/foreach-issue-trying-to-process-data-before-its-ready/19669/4 "2024-05-16T20:17:47Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


