Hello,

I have 2 csv files. I need to copy the entire 1st row (headers only) from 1 csv to another csv file and save the changes to the 2nd file.

Is there a way to automate this using a script?

12 Spice ups

Sure. What have you tried? where are you stuck?

import-csv to get the CSV / header info (or get-content) whatever you want to do

and you can use ‘out-file’ or export-csv or whatever cmdlet of your choice to export it again.

Details depend what you want it to do in the 2nd CSV file with the header info.

Give it a shot. Feel free to post the code you have tried when you get stuck and we’ll go from there.

If you post code, please use the ‘Insert Code’ button. Please and thank you!

1 Spice up

I tried the Import-csv and was kinda successful.

As an example:

File1 has the following headers: 1st_name,lst_name

File 2 has the following headers: FN,LN

All I need is for File 2 to have the same headers as File 1 and not worry about the columns information

Import-Csv -Path File1 | select header names | Export-Csv -Append -Path File2 -Force

Another option that might work, depending on how you are looking to work with the content, is using PowerQuery. This is built into Excel, and you can point to existing files, and just pull or query the content desired.
If your desire is to already be in Excel to make changes, this may be a useful method…

Hope this helps.

In that case I’d use ‘get-content’.

You can not use ‘append’ as append will add it to the end of the file.

  1. read file 1

  2. read file 2

  3. replace line 1 (or zero…) in file 2 with the info from file 1

give it a shot.

Would this work?

Get-Content File1 |
Select-Object - line1 |
Add-Content File

Meant: Add-Content File2

Did you try it? If not, create two test files and try it…

This is the error I get:

Select-Object - line1 |
| ~~~~~~~~~~~~~~~~~~~~~
| A positional parameter cannot be found that accepts argument ‘line1’.

there is no ‘line1’ property.

Select-Object -First 1

I’d do something like so…

$one = Get-Content "one.csv"
$two = Get-Content "two.csv"
$two[0] = $one[0]
$two | out-file "two.csv" -Force

I replaced the line1 with -First 1 and the script ran successfully but put the headers at the bottom of File2 and did not change line 1 of File2

Right, add-content appends, which adds to the bottom.

what’s the trick to replace the top line with the content from File1?

If I’m understanding correctly, I think this is what you’re asking for:

$Header = Get-Content -Path "Drive:\Path\FirstFile.csv" | Select-Object -First 1
$Content = Get-Content -Path "Drive:\Path\SecondFile.csv" | Select-Object -Skip 1

$Header | Set-Content -Path "Drive:\Path\NewFile.csv"
$Content | Add-Content -Path "Drive:\Path\NewFile.csv"

It’s a little more terse, but hopefully is easier to understand what and where you need to edit to complete your task

2 Spice ups
$one = Get-Content "one.csv"
$two = Get-Content "two.csv"
$two[0] = $one[0]
$two | out-file "two.csv" -Force

get-content reads an an array [0] is the first element in the array, so the first line in the file.it’s basically telling it, hey set line 1 from file 1 as line 1 in file 2

1 Spice up

that did the trick. thanks for all the help.

1 Spice up