I have path1\sample1.csv and path2\sample2.csv file with same column attributes. I need to merge both files with different path into path1\sample1.csv using PowerShell.
Something like this?
Import-CSV path\sample1.csv | Export-CSV path1\sample1.csv
Import-CSV path\sample2.csv | Export-CSV path1\sample1.csv -Append -NoTypeInformation
Related
I have a PowerShell script I run on a regular basis. Part of the script creates csv files that I later use to import data into various software programs.
The data is in a variable $enrolledStudents and the type is a System.Array
I use the following to export part of the data:
$enrolledStudents | Select-Object #{Name="SFIRST";Expression={$_.FirstName}},`
#{Name="SLAST";Expression={$_.LastName}},`
#{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
#{Name="SBIRTHDAY";Expression={$_.Birthdate }} |
Export-CSV C:\temp\Exported.csv -notype -Append
The Export looks like:
"SFIRST","SLAST","SGRADE","SBIRTHDAY"
"John","Doe","6","2009-11-22"
The software I upload the data to needs the date formatted as “11/22/2009” so it would look like:
"SFIRST","SLAST","SGRADE","SBIRTHDAY"
"John","Doe","6","11/22/2009"
Is there a way to do this in the Select-Object ?
Sure thing. Just apply a little object typing magic...
$enrolledStudents | Select-Object `
#{Name="SFIRST";Expression={$_.FirstName}},`
#{Name="SLAST";Expression={$_.LastName}},`
#{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
#{Name="SBIRTHDAY";Expression={([datetime]$_.Birthdate).ToString('MM/dd/yyyy')}} |
Export-CSV C:\temp\Exported.csv -notype -Append
Edit: Corrected the date format syntax
I now have two working solutions and wanted to add a note. Both give the same output.
# Thanks mclayton
$enrolledStudents | Select-Object #{Name="SFIRST";Expression={$_.FirstName}},`
#{Name="SLAST";Expression={$_.LastName}},`
#{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
#{Name="SBIRTHDAY";Expression={ "{1}/{2}/{0}" -f $_.Birthdate.split("-")}} |
Export-CSV C:\temp\test-Renaissance.csv -notype -Append
and
# Thanks Dennis
$enrolledStudents | Select-Object #{Name="SFIRST";Expression={$_.FirstName}},`
#{Name="SLAST";Expression={$_.LastName}},`
#{Name="SGRADE";Expression={[int]$_.GradeLevel}},`
#{Name="SBIRTHDAY";Expression={([DateTime]$_.Birthdate).ToString('MM/dd/yyyy')}} |
Export-CSV C:\temp\test-Renaissance.csv -notype -Append
I will use the second because for me, it is easier to read.
In my case it is very Important to have the [DateTime]$__.Birthdate. I found numerous examples using the .ToString but did not come across one that used the [datetime]$_
I'm trying to compare 2 csv files and highlight the duplicates or add a separate column flag with text to let them know it is a duplicate. Right now I can generate a separate csv with only the changes but I would like to keep everything in the original csv and I just wanted to see if it could be done.
Here is my code to remove the duplicates into another csv:
$csv2 = (Import-Csv -Path \.csv).Primaryemail
$removedDuplicateRows = Import-Csv -Path \.csv | Where-Object {$_.Associate_Email -in $csv2}
$removedDuplicateRows | Export-Csv -Path \.csv -NoTypeInformation
Input CSV:
CHeader1,CHeader2,CHeader3,CHeader4,CHeader5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
Output CSV:
PHeader1,PHeader2
CHeader1,CHeader2,CHeader5
a1,a2,a5
b1,b2,b5
Already tried
Import-Csv -Path .\before.csv |
select CHeader1, CHeader2, CHeader5 |
Export-Csv -Path .\after.csv
This produces the file without parent level headers.
Any suggestions to add parent level headers in first line of CSV followed by client headers and then the data?
What you're trying to create there is not actually a CSV, at least not in a way that the *-Csv cmdlets could handle. You can manually create it like this, though:
'PHeader1,PHeader2' | Set-Content '.\after.csv'
Import-Csv '.\before.csv' |
Select-Object CHeader1, CHeader2, CHeader5 |
ConvertTo-Csv -NoType |
Add-Content '.\after.csv'
I have two CSV files with the same column structure; A.csv and B.csv
I'm looking for a PowerShell way to append B.csv to the end of A.csv, without the header.
You can just try:
Import-Csv a.csv, b.csv | Export-Csv c.csv -notype
I recommend the answer with Import-Csv | Export-Csv for simplicity. Just be aware that it's probably not very efficient to parse both (potentially large) files as CSV when that's not really necessary.
This example doesn't make any assumptions about the structure of the files and just takes file 2 (without the first line) and appends it to file 1:
Get-Content .\file2.txt | where ReadCount -gt 1 >> .\file1.txt
A variation on #JPBlanc's answer:
$files=#("C:\temp\test\a.csv", "C:\temp20170219\test\b.csv")
import-csv $files | export-csv -NoType "c:\temp\c.csv"
I have CSV file MyFile.csv. It contains following data:
RollNumber Name
1 Amol
2 Ravi
Now I am fetching few records from SQL Server as follows:
RollNumber Name
3 Viku
4 Vaibhav
I am not able to find a way to append these new records to existing CSV file. I don't want to repeat header again. I don't have MS Excel, just want to play with CSV. I tried using Add-content, but it didn't worked out.
Since PowerShell v3 the Export-Csv cmdlet has an -Append parameter that allows appending to an existing CSV.
$data | Export-Csv 'MyFile.csv' -Append -NoType
On earlier versions you can work around that by converting the data to CSV, skip the header line, then append to the output file using Add-Content or Out-File -Append:
$data | ConvertTo-Csv -NoType | select -Skip 1 | Add-Content 'MyFile.csv'