# Foreach issue with .csv export

**URL:** <https://forums.powershell.org/t/foreach-issue-with-csv-export/13082>\
**Category:** PowerShell Help\
**Created:** [September 4, 2019, 7:16am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082 "2019-09-04T07:16:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)\
**Post date:** [September 4, 2019, 7:16am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/1 "2019-09-04T07:16:31Z")

</div>

Hi All,

When i use the script below the script run with out any failure.

But when i open my .csv file i only get the latest user in de file… how can i ensure that all users will be in the .csv file?

Thanks already!

```
foreach ($user in $users)
{
$test = Get-ADUser -server server1 -Identity “$user” -Credential $Cred -Properties * | select samaccountname, userprincipalname, lastlogondate
$test | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
}
```

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [September 4, 2019, 7:21am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/2 "2019-09-04T07:21:30Z")

</div>

[quote quote=174532]Hi All,

When i use the script below the script run with out any failure.

But when i open my .csv file i only get the latest user in de file… how can i ensure that all users will be in the .csv file?

Thanks already!

PowerShell

5 lines

\<textarea class="ace\_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"\>\</textarea\>

1

2

3

4

5

foreach ($user in $users)

{

$test = Get-ADUser -server server1 -Identity “$user” -Credential $Cred -Properties \* | select samaccountname, userprincipalname, lastlogondate

$test | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

[/quote] Use the -append option

&nbsp;

[pre]

Export-Csv -Append

[/pre]

---

<div class="post-metadata">

**Author:** ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)\
**Post date:** [September 4, 2019, 7:33am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/3 "2019-09-04T07:33:34Z")

</div>

Hi Bart,

Thanks for you reply, but i doesnot work.

I think my failure is already earlier in the script.

When i just do

```
foreach ($user in $users)
{
$test = Get-ADUser -server server1 -Identity “$user” -Credential $Cred -Properties * | select samaccountname, userprincipalname, lastlogondate}
```

Also my $test has just got one user in it, instead of many user… what do i do wrong?

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [September 4, 2019, 7:53am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/4 "2019-09-04T07:53:06Z")

</div>

Ok little example

&nbsp;

[pre]  
$t = 1…10  
$d = @()

foreach ($item in $t)  
{  
$item  
$d += $item  
}

$d | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation  
[/pre]

---

<div class="post-metadata">

**Author:** ![j2sankha](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@j2sankha](https://forums.powershell.org/u/j2sankha)\
**Post date:** [September 4, 2019, 8:07am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/5 "2019-09-04T08:07:11Z")

</div>

Hi,

Use convertto-csv to export the output.

Ex.

[pre]

$test|convertto-csv | out-file .\test.csv -append

[/pre]

There is also another way, but I hope it will work. Plz check and let us know.

---

<div class="post-metadata">

**Author:** ![jasonrobertson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jasonrobertson/32/164_2.png) [@jasonrobertson](https://forums.powershell.org/u/jasonrobertson)\
**Post date:** [September 4, 2019, 10:42am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/6 "2019-09-04T10:42:02Z")

</div>

Paul1984,

Here you go.

```
$users = 'Jason.Robertson','Jason.Test'

$Test = New-Object -TypeName System.Collections.Hashtable

foreach ($user in $users)

{

$test+=Get-ADUser-Identity $user-Properties LastLogonDate |Select-Object samaccountname, userprincipalname, lastlogondate

}

$test | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![aaron-hardy](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aaron-hardy](https://forums.powershell.org/u/aaron-hardy)\
**Post date:** [September 4, 2019, 11:38am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/7 "2019-09-04T11:38:57Z")

</div>

What I normally do is similar to Jason.Robertson but instead of concatenating each object into a variable inside a loop, I assign the foreach output to a variable and pipe it to Export-Csv:

```
$userCollection = foreach ($user in $users)
{
    Get-ADUser -server server1 -Identity $user -Credential $Cred -Properties LastLogonDate | select samaccountname, userprincipalname, lastlogondate
}

$userCollection | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![jasonrobertson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jasonrobertson/32/164_2.png) [@jasonrobertson](https://forums.powershell.org/u/jasonrobertson)\
**Post date:** [September 4, 2019, 3:31pm UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/8 "2019-09-04T15:31:27Z")

</div>

@Aaron,

Its great learning new ways to collect and store the data. I use this for my condition statements, don’t know why it never occurred to me do to that for the Foreach loop.

&nbsp;

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [September 10, 2019, 8:26am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/9 "2019-09-10T08:26:42Z")

</div>

how about…

```
$users | %{Get-ADUser -server server1 -Identity $\_ -Credential $Cred -Properties LastLogonDate | select samaccountname, userprincipalname, lastlogondate} | Export-Csv -Path C:\temp\allusers2.csv -NoTypeInformation -append
```

---

<div class="post-metadata">

**Author:** ![jasonrobertson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jasonrobertson/32/164_2.png) [@jasonrobertson](https://forums.powershell.org/u/jasonrobertson)\
**Post date:** [September 10, 2019, 10:29am UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/10 "2019-09-10T10:29:02Z")

</div>

Hello Ian,

This would work just fine if you are needing to run a one line, however if you writing a script the proper structure would be to use a foreach loop instead. I personally would not add the -append to the Export-Csv unless explicitly requested by the requestor as we don’t know the expectation of the export. It could very well be that Paul expects the data to be overwritten each time. 🙂

---

<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:31pm UTC](https://forums.powershell.org/t/foreach-issue-with-csv-export/13082/11 "2024-05-16T20:31:23Z")

</div>

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


