Time stamp every entry

Welcome Forums General PowerShell Q&A Time stamp every entry

This topic contains 5 replies, has 5 voices, and was last updated by

1 year, 6 months ago.

  • Author
  • #76864

    Points: 26
    Rank: Member


    i have a script that outputs a line of results to a CSV, i would like to add a date stamp to a column each time the entry is created.

    Any ideas?



  • #76879

    Points: 1
    Rank: Member

    This is what I use:

    $TimeStamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')>/pre>
  • #76899

    Points: 26
    Rank: Member

    Thanks Don. I've already got that part, but what im trying to do is to add the value into the csv along side the export-csv.
    So kinda like

    Do-Something | Export-CSV including $DateTime. 


  • #76902

    Points: 64
    Rank: Member

    I created a csv with a column of names in column 1 (heading name) then ran the following which creates another csv but with the date time in column 2

    $Userlist=Import-Csv "c:\test\name.csv"
    Foreach ($user in $userlist)
    $obj = New-Object System.Object
    $obj | Add-Member -MemberType NoteProperty -name 'Username' -value $User.name
    $obj | Add-Member -MemberType NoteProperty -name 'date' -value (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
    $results += $obj

    $results | export-csv c:\test\withdate.csv

  • #76908

    Points: 21
    Rank: Member

    Do-Something | Add-Member -MemberType NoteProperty -Name Timestamp -Value (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') -Passthru | export-csv -path myfile.csv

    Hope this helps

  • #76915

    Points: 176
    Helping Hand
    Rank: Participant

    This generates an example of your single liner (single column) CSV:

    $myCSV = '.\mydatatest.csv'
    0..9 | % { [PSCustomObject]@{ Data = "bla$_" } } | # Example of your existing 10 lines of data
        Export-Csv $myCSV -NoType                      # Saved to CSV

    This adds a 'TimeStamp' column to existing rows:

    $myData = Import-Csv $myCSV |                                                 # Read data from CSV
        Add-Member -MemberType NoteProperty -Name 'TimeStamp' -Value '' -PassThru # Add TimeStamp column
    $myData | Export-Csv $myCSV -NoType                                           # Save it back to CSV

    Example code for future time stamped entries:

    $newEntry = [PSCustomObject]@{ 
        Data      = "bla and some $Variables" 
        TimeStamp = Get-Date -Format 'ddMMMMyyyy_hh-mm-ss_tt'
    $newEntry | Export-Csv $myCSV -NoType -Append  

The topic ‘Time stamp every entry’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort