0

I am trying to join together two csv's. One of the CSVs contain the column I want to add to the other but when the csv is exported the column added is blank.

$csv1

VLAN
1
2
3

$csv2

Host
NETMAN
ADMIN
CLIENT

I get

VLAN, Host
1,
2,
3,

Desired output is

VLAN, Host
1,NETMAN
2,ADMIN
3,CLIENT

I tried

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$csv1 | % {$i=0} { $csv1[$i] | Add-Member -NotePropertyName Hostname -NotePropertyValue $csv2[$i++].Hostname;}

$csv1 | Export-Csv combined.csv -NoType
iRon
  • 20,463
  • 10
  • 53
  • 79
ShanayL
  • 1,217
  • 2
  • 14
  • 29
  • Does this answer your question? [In PowerShell, what's the best way to join two tables into one?](https://stackoverflow.com/questions/1848821/in-powershell-whats-the-best-way-to-join-two-tables-into-one) – TylerH May 23 '22 at 13:14

2 Answers2

2

here you go:

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$arr = @()
#If($csv1.count -eq $csv2.count){
    for($i=0; $i -lt $csv1.Count; $i++){
        $temp = New-Object psobject
        $temp | Add-Member -MemberType NoteProperty -Name VLAN -Value $csv1[$i].Vlan
        $temp | Add-Member -MemberType NoteProperty -Name HOST -Value $csv2[$i].Host
        $arr+=$temp
    }
#}

$arr | Export-Csv combined.csv -NoTypeInformation
Paul
  • 5,524
  • 1
  • 22
  • 39
  • I am still coming out blank... I'm not sure why... I thought it may have been the host file so I recreated it. – ShanayL Apr 12 '16 at 14:29
  • @slong16 is the whole file blank or only one column? If you Output $arr what does it Show? – Paul Apr 12 '16 at 14:33
  • 1
    OMG I feel so stupid, my original code worked. This entire time I left off the 's' in 'hostname'... Your code worked as well once I put the s on it. I was not paying attention. My original csv has the header hostnames.I had been working this for about 45 min. – ShanayL Apr 12 '16 at 14:40
0

$csv1 | Join $csv2

Results

VLAN Host
---- ----
1    NETMAN
2    ADMIN
3    CLIENT
iRon
  • 20,463
  • 10
  • 53
  • 79