Powershell script to add a date to each row of a csv.

  • Thread starter Thread starter Killer_3669
  • Start date Start date
K

Killer_3669

Guest
Hi all, struggling to get a powershell script working so I'm wondering if somebody would be kind enough to help, hopefully it's something simple but my experience with powershell isnt that great although I am learning heaps.


I have a simple database (a csv file) called sample transactions.csv which has 5 cols as shown below, and I need to append the date to the end of each row which will make 6 cols.


000002,1 ,0.00 ,PRN,MSN Australia | latest news, Hotmail login, Outlook, Skype and Video

000002,2 ,0.00 ,PRN,MSN Australia | latest news, Hotmail login, Outlook, Skype and Video


Below is my powershell script and is how I've done it so far and its outputs correctly on screen if I remove the:


|Export-Csv -Path C:\Filepro\Exports\sample_transactions_inc_header_date.csv -NoTypeInformation


but if I keep that line in, the file that's finally outputted only has a single line but unsure what I'm doing wrong, apart from that minor issue of it not working this is exactly what I need :)



powershell Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

#
#
######### Adding the headers #############
Import-Csv C:\Filepro\Exports\sample_transactions.csv -Header A,B,C,D,E | sort A |Export-Csv -Path C:\Filepro\Exports\sample_transactions_inc_header.csv -NoTypeInformation
#
#
######### Adding date as another col F #############
Import-Csv C:\Filepro\Exports\sample_transactions_inc_header.csv | foreach-object {
$row = $_
$row | add-member NoteProperty "F" (Get-Date -format "dd/MM/yyyy")
$row |Export-Csv -Path C:\Filepro\Exports\sample_transactions_inc_header_date.csv -NoTypeInformation
}
######## I'm missing something regarding the above line ##########
#
# $row |Export-Csv -Path C:\Filepro\Exports\sample_transactions_inc_header_date.csv -NoTypeInformation
#
######## Removing the header row ###########
Get-Content C:\Filepro\Exports\sample_transactions_inc_header_date.csv|select -Skip 1|Set-Content C:\Filepro\Exports\sample_transactions_header_removed.csv
#
#
#########Adding Time Date to filename and saving #############
Rename-Item -Path C:\Filepro\Exports\sample_transactions_header_removed.csv -newname C:\Filepro\Exports\FilePro_export_$(Get-Date -F yyyy-MM-dd_HH-mm).csv
#
#
########## Removing temp files #############
Remove-Item –path C:\Filepro\Exports\sample_transactions_inc_header.csv

Remove-Item –path C:\Filepro\Exports\sample_transactions_inc_header_date.csv



Any help would be truly appreciated, cheers Rich

Continue reading...
 
Back
Top