# Export to CSV - It Writes Only Last Row

**URL:** <https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288>\
**Category:** PowerShell Help\
**Created:** [August 12, 2021, 12:05am UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288 "2021-08-12T00:05:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renoir](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@renoir](https://forums.powershell.org/u/renoir)\
**Post date:** [August 12, 2021, 12:05am UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288/1 "2021-08-12T00:05:20Z")

</div>

My friends,  
I am having trouble exporting to a CSV file. Only the last row is written to the export file, Only the last one.  
I would like to import a CSV file of CN Names and  
Find the names in Active Directory and get SamAccountName, mail and  
Export to a CSV File.

Import File: C:\Folderx\BulkShort.cvs  
User  
Hun, Atilla  
Borden, Lizzie  
Capone, Al

Results, I get results for only the last row of the import file.  
Export File:  
C:\Folderx\Sammail.cvs  
SamAccountName Mail  
Al.Capone [Al.Capone@Atlanta.com](mailto:Al.Capone@Atlanta.com)

Here’s the script:

```auto
  Import-Csv -Path "C:\Folderx\BulkShort.csv" |           
foreach {           
 Get-ADUser -SearchBase "OU=users,OU=Defense,OU=Agencies,DC=Fed,DC=net" -Filter "cn -like '*$($_.user)*'" -Properties * | select SamAccountName, mail |   
  Export-Csv -Path C:\Folderx\Sammail.csv 
}

```

If I don’t use the -searchbase, it runs for hours with two-hundred names because our Active Directory is so big.  
Thanks in advance,

---

<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:** [August 12, 2021, 6:00am UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288/2 "2021-08-12T06:00:55Z")

</div>

Actually it does write all of the accounts to the CSV file but since you have the export inside the loop it overwrites each one of them except of the last one.

To circumvent this you could simply add the paramter -Append to your Export-Csv command. A better option would be to move the export outside the loop.

```auto
$SearchBase = 'OU=users,OU=Defense,OU=Agencies,DC=Fed,DC=net'

Import-Csv -Path 'C:\Folderx\BulkShort.csv' |
    ForEach-Object {           
        Get-ADUser -SearchBase $SearchBase -Filter "cn -like '*$($_.user)*'" -Properties mail | 
            Select-Object sAMAccountName, mail 
    }|   
        Export-Csv -Path 'C:\Folderx\Sammail.csv' 

```

---

<div class="post-metadata">

**Author:** ![renoir](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@renoir](https://forums.powershell.org/u/renoir)\
**Post date:** [August 12, 2021, 2:43pm UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288/3 "2021-08-12T14:43:20Z")

</div>

Olaf, Thank you. Yes, that works. I looked for hours but for some reason, I could not find the simple solution.  
My co-worker and I will be adding to this script to modify some Attributes, moving the record to a different OU and disabling the records. We decided to write this script because four or five times per year we get a bulk request with hundreds of names.  
I will keep you informed.  
Many Thanks, Olaf

---

<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:** [August 12, 2021, 2:53pm UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288/4 "2021-08-12T14:53:04Z")

</div>

> [@renoir](#):
>
> Yes, that works.

Great. I’m glad it helped. 👍🏽

You may mark my answer as the solution. This might help others looking for the same or a similar problem finding the solution a little quicker. 😉

---

<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:22pm UTC](https://forums.powershell.org/t/export-to-csv-it-writes-only-last-row/17288/5 "2024-05-16T20:22:01Z")

</div>

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


