# Importing extra column from CSV and exporting it to a CSV

**URL:** <https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896>\
**Category:** PowerShell Help\
**Created:** [July 21, 2019, 8:53am UTC](https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896 "2019-07-21T08:53:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [July 21, 2019, 8:53am UTC](https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896/1 "2019-07-21T08:53:57Z")

</div>

Hello Everyone,

I am Importing a CSV (test.csv)with usernames in 1 column and other Information in other column and pulling Information from AD based on those usernames, which I can export to a CSV no problem, but I also want to keep another column from(test.csv) as it is and want to export it to my (result.csv) and I am unable to do that. Here is my code.

$testcsv = Import-csv -path “C:\Users\username\Documents\Script\TestFolder\test.csv”

ForEach($user in $testcsv)  
{  
$usercsv = $user.user

get-aduser -filter {samaccountname -eq $usercsv} -properties \* |  
select-object NAME,whencreated,whenchanged ,@{Name=“Time Difference” ;Expression = {(New-TimeSpan -Start $_.whencreated -end $_.whenchanged).TotalDays}}, `  
@{Name=“AnotherColumn” ; Expression = {$testcsv | Select-Object -property other}}

}

---

<div class="post-metadata">

**Author:** ![nitbha](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@nitbha](https://forums.powershell.org/u/nitbha)\
**Post date:** [July 21, 2019, 9:18am UTC](https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896/2 "2019-07-21T09:18:59Z")

</div>

@[aman](https://powershell.org/profile/aman1313/)  
You can covert your CSV file input into PowerShell Customer Object first where you can take all the headers from csv which you need and then add the logic for pulling Information from AD.

```
Example:[https://stackoverflow.com/questions/48270462/creating-a-custom-powershell-object-from-csv-input?answertab=active#tab-top](https://stackoverflow.com/questions/48270462/creating-a-custom-powershell-object-from-csv-input?answertab=active#tab-top)
```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [July 21, 2019, 4:41pm UTC](https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896/3 "2019-07-21T16:41:13Z")

</div>

If I understand correctly, you want to export (3) columns per user from test.csv, add AD info then export the results. Below should work for you.  
I put the results of the loop in a variable then exported the variable contents to csv.

TIP:  
No need for username in variable or to list all AD properties

```
$testcsv = Import-csv -path "C:\Users\username\Documents\Script\TestFolder\test.csv"

$result =
ForEach($user in $testcsv) {
  Get-Aduser -filter {samaccountname -eq $user.user} -properties whencreated,whenchanged |
  Select-Object @{Name='User';exp={$user.user}},NAME,whencreated,whenchanged,
  @{Name="Time Difference" ;Expression = {(New-TimeSpan -Start $_.whencreated -end $_.whenchanged).TotalDays}}, `
  @{Name="AnotherColumn" ; Expression = {$user.other}}
}

$result | Export-Csv -NoTypeInformation -Path .\testresult.csv
```

---

<div class="post-metadata">

**Author:** ![aman1313](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@aman1313](https://forums.powershell.org/u/aman1313)\
**Post date:** [July 22, 2019, 8:51am UTC](https://forums.powershell.org/t/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896/4 "2019-07-22T08:51:08Z")

</div>

Thank You

---

<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/importing-extra-column-from-csv-and-exporting-it-to-a-csv/12896/5 "2024-05-16T20:31:55Z")

</div>


