Adding a DateTime column to data before exporting to CSV

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.

$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.

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

$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 ';'
1 Like

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.