\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask! \n\n\nUse a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which. \n\n\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…\n <\/blockquote>\n<\/aside>\n","upvoteCount":1,"datePublished":"2023-02-14T17:07:49.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I tried the Import-csv and was kinda successful.<\/p>\n
As an example:<\/p>\n
File1 has the following headers: 1st_name,lst_name<\/p>\n
File 2 has the following headers: FN,LN<\/p>\n
All I need is for File 2 to have the same headers as File 1 and not worry about the columns information<\/p>\n
Import-Csv -Path File1 | select header names | Export-Csv -Append -Path File2 -Force\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-02-14T17:24:14.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/3","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"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. \nIf your desire is to already be in Excel to make changes, this may be a useful method…<\/p>\n
Hope this helps.<\/p>","upvoteCount":0,"datePublished":"2023-02-14T17:24:16.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/4","author":{"@type":"Person","name":"sbux2k","url":"https://community.spiceworks.com/u/sbux2k"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-31qkz:<\/div>\n
\nI tried the Import-csv and was kinda successful.<\/p>\n
As an example:<\/p>\n
File1 has the following headers: 1st_name,lst_name<\/p>\n
File 2 has the following headers: FN,LN<\/p>\n
All I need is for File 2 to have the same headers as File 1 and not worry about the columns information<\/p>\n
Import-Csv -Path File1 | select header names | Export-Csv -Append -Path File2 -Force\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nIn that case I’d use ‘get-content’.<\/p>\n
You can not use ‘append’ as append will add it to the end of the file.<\/p>\n
\n\nread file 1<\/p>\n<\/li>\n
\nread file 2<\/p>\n<\/li>\n
\nreplace line 1 (or zero…) in file 2 with the info from file 1<\/p>\n<\/li>\n<\/ol>\n
give it a shot.<\/p>","upvoteCount":0,"datePublished":"2023-02-14T17:30:57.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Would this work?<\/p>\n
Get-Content File1 |\nSelect-Object - line1 |\nAdd-Content File\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-02-14T17:43:05.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/6","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"Meant: Add-Content File2<\/p>","upvoteCount":0,"datePublished":"2023-02-14T17:45:21.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/7","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-31qkz:<\/div>\n
\nWould this work?<\/p>\n
Get-Content File1 |\nSelect-Object - line1 |\nAdd-Content File\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nDid you try it? If not, create two test files and try it…<\/p>","upvoteCount":0,"datePublished":"2023-02-14T17:47:49.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
This is the error I get:<\/p>\n
Select-Object - line1 | \n| ~~~~~~~~~~~~~~~~~~~~~ \n| A positional parameter cannot be found that accepts argument ‘line1’.<\/p>","upvoteCount":0,"datePublished":"2023-02-14T17:55:36.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/9","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-31qkz:<\/div>\n
\nThis is the error I get:<\/p>\n
Select-Object - line1 | \n| ~~~~~~~~~~~~~~~~~~~~~ \n| A positional parameter cannot be found that accepts argument ‘line1’.<\/p>\n<\/blockquote>\n<\/aside>\n
there is no ‘line1’ property.<\/p>\n
Select-Object -First 1\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-02-14T17:58:20.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/10","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I’d do something like so…<\/p>\n
$one = Get-Content \"one.csv\"\n$two = Get-Content \"two.csv\"\n$two[0] = $one[0]\n$two | out-file \"two.csv\" -Force\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2023-02-14T17:58:58.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/11","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I replaced the line1 with -First 1<\/strong> and the script ran successfully but put the headers at the bottom of File2 and did not change line 1 of File2<\/p>","upvoteCount":0,"datePublished":"2023-02-14T18:06:56.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/12","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-31qkz:<\/div>\n
\nI replaced the line1 with -First 1<\/strong> and the script ran successfully but put the headers at the bottom of File2 and did not change line 1 of File2<\/p>\n<\/blockquote>\n<\/aside>\nRight, add-content appends, which adds to the bottom.<\/p>","upvoteCount":0,"datePublished":"2023-02-14T18:09:54.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/13","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
what’s the trick to replace the top line with the content from File1?<\/p>","upvoteCount":0,"datePublished":"2023-02-14T18:11:26.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/14","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}},{"@type":"Answer","text":"
If I’m understanding correctly, I think this is what you’re asking for:<\/p>\n
$Header = Get-Content -Path \"Drive:\\Path\\FirstFile.csv\" | Select-Object -First 1\n$Content = Get-Content -Path \"Drive:\\Path\\SecondFile.csv\" | Select-Object -Skip 1\n\n$Header | Set-Content -Path \"Drive:\\Path\\NewFile.csv\"\n$Content | Add-Content -Path \"Drive:\\Path\\NewFile.csv\"\n<\/code><\/pre>\nIt’s a little more terse, but hopefully is easier to understand what and where you need to edit to complete your task<\/p>","upvoteCount":2,"datePublished":"2023-02-14T18:22:56.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/15","author":{"@type":"Person","name":"saidbrandon","url":"https://community.spiceworks.com/u/saidbrandon"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-31qkz:<\/div>\n
\nwhat’s the trick to replace the top line with the content from File1?<\/p>\n<\/blockquote>\n<\/aside>\n
$one = Get-Content \"one.csv\"\n$two = Get-Content \"two.csv\"\n$two[0] = $one[0]\n$two | out-file \"two.csv\" -Force\n<\/code><\/pre>\nget-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<\/p>","upvoteCount":1,"datePublished":"2023-02-14T18:25:06.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/16","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
that did the trick. thanks for all the help.<\/p>","upvoteCount":1,"datePublished":"2023-02-14T18:53:05.000Z","url":"https://community.spiceworks.com/t/scripting-ideas/946258/17","author":{"@type":"Person","name":"spiceuser-31qkz","url":"https://community.spiceworks.com/u/spiceuser-31qkz"}}]}}
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
Neally
(Neally)
February 14, 2023, 5:07pm
2
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!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…
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
sbux2k
(SBUX2K)
February 14, 2023, 5:24pm
4
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.
Neally
(Neally)
February 14, 2023, 5:30pm
5
spiceuser-31qkz:
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
In that case I’d use ‘get-content’.
You can not use ‘append’ as append will add it to the end of the file.
read file 1
read file 2
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
Neally
(Neally)
February 14, 2023, 5:47pm
8
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’.
Neally
(Neally)
February 14, 2023, 5:58pm
10
there is no ‘line1’ property.
Select-Object -First 1
Neally
(Neally)
February 14, 2023, 5:58pm
11
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
Neally
(Neally)
February 14, 2023, 6:09pm
13
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
Neally
(Neally)
February 14, 2023, 6:25pm
16
$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