[SOLVED] How to copy column and its values from one csv and paste to another csv

Issue

This Content is from Stack Overflow. Question asked by aqsa

I have 2 csv files.
Csv 1 contains information about HostName,Adstatus,LastLogonDate
Csv 2 contains Information about Hostname,Sccmstatus.
The value of HostName is same in both the files.
I want to copy Sccmstatus column from Csv2 and paste it in Csv1 file .
Is that possible ?

Thank you for the help .



Solution

my approach would be:

$csv = Import-Csv "C:\stackoverflow\csv1.csv" -Delimiter ","
$csv | Add-Member -NotePropertyName "Sccmstatus" -NotePropertyValue ""
Import-Csv  "C:\stackoverflow\csv2.csv" -Delimiter "," | ForEach-Object {
$hostname=$_.HostName
$status=$_.Sccmstatus
($csv | ? {$_.HostName -eq $hostname}).Sccmstatus=$status 
}
$csv | Export-Csv -Path "C:\stackoverflow\csvResult.csv" -Delimiter "," -Encoding UTF8 -Force -NoTypeInformation


This Question was asked in StackOverflow by aqsa and Answered by User It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?