Search code examples
powershellappendexport-csv

Adding a 'Date Modified' column that will populate the date the .csv is appended


I am exporting an array of data to a .csv file when my script detects a certain condition for a device and then deletes the device from the console. The .csv is a record of the deleted device info, and I'm using Export-Csv with the -append command to add a line every time this happens, and I'd like to have the script add a date column when this action happens, but I'm having trouble figuring out how to write this. The .csv file currently has columns for strings in the array, like 'owner', 'model', 'phone number', etc.

I have tried many options I've found via Internet searches, but nothing seems to be working.

Foreach ($item in $noncomp60){

    $nc60details += $noncomp | ? {$_.Serial -eq $item}
    $nc60details | Export-Csv -Path $path -NoTypeInformation -Append

I'd like the .csv file to have an addition column titled 'Date Deleted' which is populated when the .csv is appended.


Solution

  • Try adding this line before Export-Csv

    $nc60details | Add-Member -Name 'DateDeleted' -Value (get-date) -MemberType NoteProperty