3

I want to add time and date row at the end of each entry in the csv file. I am only able to add time and date at the end of the database not to each row

Powershell

import-csv C:\Users\system\Desktop\WindowsUpdate1.csv | 
  foreach-object {
    $row = $_
    $row | add-member NoteProperty "Time" (Get-Date -format t) 
    $row 
  }

I only get one column in regard to the date and time instead of getting it in every row.

2
  • 2
    try using a calculated property with Select-Object. Commented Sep 18, 2019 at 9:20
  • 3
    Aside from the fact that Select-Object with calculated properties would be a better solution, as @Lee_Dailey notes, your code looks fine and should successfully add a .Time property to each input object (row). Commented Sep 18, 2019 at 9:31

1 Answer 1

2

As Lee_Dailey commented, the easiest way is to use the Select-Object cmdlet with a calculated property:

$newCsv = Import-Csv 'C:\Users\system\Desktop\WindowsUpdate1.csv' | 
    Select-Object *, @{Name = 'Time'; Expression = {(Get-Date -format s)}}

#output on screen
$newCsv

#output to new csv file
$newCsv | Export-Csv 'C:\Users\system\Desktop\WindowsUpdate2.csv' -NoTypeInformation

I have changed your date/time format from (Get-Date -format t) to (Get-Date -format s) in order to get a sortable date/time format because the title of the post asks for date AND time. See Standard Date and Time formats for more formats

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.