Time stamp every entry

This topic contains 5 replies, has 5 voices, and was last updated by  Sam Boutros 1 week, 4 days ago.

  • Author
    Posts
  • #76864

    TommyQuality
    Participant

    Hi,

    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?

    thanks

    Tommy.

  • #76879

    Don Reese
    Participant

    This is what I use:

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

    TommyQuality
    Participant

    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. 

    cheers

  • #76902

    Simon B
    Participant

    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

    $obj=@()
    $results=@()
    $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

    Fredrik Lindberg
    Participant

    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
    /fridden

  • #76915

    Sam Boutros
    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  
    

You must be logged in to reply to this topic.