# Help with a ForEach

**URL:** <https://forums.powershell.org/t/help-with-a-foreach/11552>\
**Category:** PowerShell Help\
**Created:** [October 25, 2018, 4:05pm UTC](https://forums.powershell.org/t/help-with-a-foreach/11552 "2018-10-25T16:05:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [October 25, 2018, 4:05pm UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/1 "2018-10-25T16:05:12Z")

</div>

I have a csv of a list of SamAccountNames. Several of these users have a blank EmailAddress Property. This is my code:

```
$Users = Import-Csv "C:\Users.csv"

foreach ( $User in $Users ) {
Get-ADUser -Properties * -Identity $User.SamAccountName | Where { $_.EmailAddress -eq $Null } | Select Name, SamAccountName | Export-Csv C:\Users\a033904\Documents\DeptNoEmail.csv }
```

If I remove the Export-Csv part and run it I get the desired results I need but with the Export-Csv in the Csv all I get is the last user of the imported Csv. Does that make sense to you? I don’t quite understand what is wrong. Any guidance would be greatly appreciated.

Rich

---

<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:** [October 25, 2018, 4:47pm UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/2 "2018-10-25T16:47:22Z")

</div>

You have 2 options. Either you add the parameter **-Append** to your Export-CSV - but that slows down your code a lot - or you place your Export-CSV outside of your loop.

---

<div class="post-metadata">

**Author:** ![wes-stahler](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wes-stahler/32/227_2.png) [@wes-stahler](https://forums.powershell.org/u/wes-stahler)\
**Post date:** [October 25, 2018, 7:42pm UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/3 "2018-10-25T19:42:24Z")

</div>

[pre]

$Users = Import-Csv “C:\Users.csv”

$Users | ForEach-Object -Begin {  
$output=@()  
} -Process {  
$output += Get-ADUser -Identity $\_.samaccountname | Select-Object Name, SamAccountName  
} -End {  
$output | Export-Csv C:\temp\DeptNoEmail1.csv -NoTypeInformation  
}

[/pre]

---

<div class="post-metadata">

**Author:** ![jseerden](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jseerden](https://forums.powershell.org/u/jseerden)\
**Post date:** [October 26, 2018, 1:50am UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/4 "2018-10-26T01:50:17Z")

</div>

You are recreating the Csv in the Export-Csv PowerShell cmdlet within the ForEach loop. It is overwritten each loop.

If you take it outside the loop, it can contain all user objects.

[pre]  
$Users = Import-Csv “C:\Users.csv”

$UserObjects = foreach ($User in $Users) {  
Get-ADUser -Properties \* -Identity $User.SamAccountName |  
Where-Object { $\_.EmailAddress -eq $null } |  
Select-Object Name, SamAccountName  
}

$UserObjects | Export-Csv C:\Users\a033904\Documents\DeptNoEmail.csv  
[/pre]

&nbsp;

---

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [October 26, 2018, 5:10am UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/5 "2018-10-26T05:10:44Z")

</div>

Thank you everyone for the help. Coming into work this morning my mind was not so scrambled trying to figure this out. This is what I ended up using.

```
Get-Content C:\Powershell\Scripts\O365\Migration\DeptUsers.txt | Get-ADUser -Properties * | Where { $_.EmailAddress -eq $Null } | Select Name, EmailAddress | Export-Csv C:\NoEmail.csv -NotypeInformation
```

Powershell can be a love hate relationship, but I have to say I do enjoy working through the problems.

&nbsp;

Rich

---

<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:34pm UTC](https://forums.powershell.org/t/help-with-a-foreach/11552/6 "2024-05-16T20:34:11Z")

</div>


