-1

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 .

  • Check out the answers on this similar question [Copy columns from one csv file to another using PowerShell](https://stackoverflow.com/questions/64843697/copy-columns-from-one-csv-file-to-another-using-powershell) – K-Kns Sep 18 '22 at 12:29
  • Yep, that could be done, but.. where is the code you have already tried so we can help with that? – Theo Sep 18 '22 at 12:30
  • $Content1=import-csv "c:\temp\1.csv" -Delimiter "," $Content2=import-csv "c:\temp\2.csv" -Delimiter "," $MemberToGet=Get-Member -InputObject $Content2[0] -MemberType NoteProperty | sort HostName $i=-1 $Content1 | %{ $CurrentRowObject=$_ $i++ $MemberToGet | %{ $Name=$_.Name Add-Member -InputObject $CurrentRowObject -MemberType NoteProperty -Name $Name -Value $Content2[$i]."$Name" -Force } #send to output the result object $CurrentRowObject } | export-csv "c:\temp\3.csv". – aqsa Sep 18 '22 at 12:39
  • Also after executing the code column Sccmstatus has been added but there are no values in it – aqsa Sep 18 '22 at 12:41
  • 1
    Please [edit] your question and add your code as formatted text there. In a comment it becomes unreadable – Theo Sep 18 '22 at 14:12
  • Assuming that there is a relation between the two `HostName` columns where they need to be the same, using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `Import-Csv .\1.csv |Join (Import-Csv .\2.csv) -on HostName` – iRon Sep 19 '22 at 14:41
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 19 '22 at 23:30

1 Answers1

0

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
User
  • 26
  • 1
  • 5