# Adding a DateTime column to data before exporting to CSV

**URL:** <https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874>\
**Category:** PowerShell Help\
**Created:** [March 22, 2022, 2:43pm UTC](https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874 "2022-03-22T14:43:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [March 22, 2022, 2:43pm UTC](https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874/1 "2022-03-22T14:43:05Z")

</div>

I’ve got a script which exports a CSV of the mailboxes receiving the highest number of external mail.  
The CSV only contains the number of external emails in the last day and the receiving e-mailaddress.

```auto
$Days = 7
$CSV = "$env:TEMP\$(Get-Date -Format "yyyyMMdd")-$($Days)-AddressList.csv"

$rAdds = $messageList | Select-Object -ExpandProperty RecipientAddress | Where-Object -FilterScript {$_ -like '*@[MAIL-SUFFIX]'}

$rAdds | Group-Object | Sort-Object -Property Count -Descending | Where-Object -FilterScript {$_.Count -gt 10 } | Select-Object -Property Count, Name | Export-Csv -Path $CSV -NoTypeInformation -Encoding utf8 -Delimiter ';'

```

I’ve been asked to add a Date or DateTime column to the CSV for easier handling in PowerBI and I’m honestly embarrassed that I’m not sure exactly how to go about that. Basically they want `Get-Date -Format yyyy-MMdd` added to each entry.  
I’ve found a number of topics about adding a column to an existing CSV - and I guess I could reimport the CSV and modify it before exporting it again. I really think there should be an easier way of just adding it before the export.

---

<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:** [March 22, 2022, 2:58pm UTC](https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874/2 "2022-03-22T14:58:03Z")

</div>

I’m nut sure if I really got it … if I got it you may try this:

```auto
$TimeStamp = Get-Date -Format 'yyyy-MMdd'
$rAdds | 
    Group-Object | 
        Sort-Object -Property Count -Descending | 
            Where-Object -FilterScript { $_.Count -gt 10 } | 
                Select-Object -Property Count, Name, @{Name = 'TimeStamp'; Expression = {$TimeStamp}} | 
                    Export-Csv -Path $CSV -NoTypeInformation -Encoding utf8 -Delimiter ';'

```

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [March 22, 2022, 3:11pm UTC](https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874/3 "2022-03-22T15:11:59Z")

</div>

Thank you, that was exactly what I needed.  
I suspected that I would need a calculated property, but was unsure how/where to add it in the pipeline.

Think I had convinced myself that Select-Object only could act on the piped in properties.

---

<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:18pm UTC](https://forums.powershell.org/t/adding-a-datetime-column-to-data-before-exporting-to-csv/18874/4 "2024-05-16T20:18:09Z")

</div>

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


