# Combine 2 collections into one for CSV file export

**URL:** <https://forums.powershell.org/t/combine-2-collections-into-one-for-csv-file-export/22671>\
**Category:** PowerShell Help\
**Created:** [July 28, 2023, 9:20am UTC](https://forums.powershell.org/t/combine-2-collections-into-one-for-csv-file-export/22671 "2023-07-28T09:20:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JDMils](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@JDMils](https://forums.powershell.org/u/JDMils)\
**Post date:** [July 28, 2023, 9:20am UTC](https://forums.powershell.org/t/combine-2-collections-into-one-for-csv-file-export/22671/1 "2023-07-28T09:20:44Z")

</div>

I’m starting with this code:

```auto
Get-UcsServer | Get-UcsFabricLocale | Get-UcsFabricPath | Get-UcsDcxVc | Select dn,switchid,operstate,adminstate,vnic,linkstate | Where { $_.vnic } | Sort dn | ft

```

It does the job nicely, however there are properties in UCSServer, UCSFabricLocale, UCSFabrixPath and UCSDcxVc which I would like to add to the output and then export the results to CSV.

I expanded the code to this behemoth:

```auto

$AllUCSPaths = @()
$AllUCSServers = Get-UcsServer
ForEach ($UCSServer in $AllUCSServers)
    {
    $UCSFabricLocales = $UCSServer | Get-UcsFabricLocale
    $Item1 = New-Object PSObject
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_AdminState -Value $UCSServer.AdminState
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_AssignToDN -Value $UCSServer.AssignedToDN
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Associateion -Value $UCSServer.Association
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Availability -Value $UCSServer.Availability
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_ChassisID -Value $UCSServer.ChassisID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Discovery -Value $UCSServer.Discovery
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_DiscoverStatus -Value $UCSServer.DiscoverStatus
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Model -Value $UCSServer.Model
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Name -Value $UCSServer.Name
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_NumOfAdapters -Value $UCSServer.NumOfAdapters
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_OperPower -Value $UCSServer.OperPower
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_OperState -Value $UCSServer.OperState
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Operability -Value $UCSServer.Operability
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Serial -Value $UCSServer.Serial
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_ServerID -Value $UCSServer.ServerID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_SLotID -Value $UCSServer.SLotID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_UsrLbl -Value $UCSServer.UsrLbl
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Vendor -Value $UCSServer.Vendor
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_DN -Value $UCSServer.DN
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_RN -Value $UCSServer.RN
    ForEach ($UCSFabricLocale in $UCSFabricLocales)
        {
        $AllUCSFabricPaths = $UCSFabricLocale | Get-UcsFabricPath
        ForEach ($UCSFabricPath in $AllUCSFabricPaths)
            {
            $AllUCSDcxVc = $UCSFabricPath | Get-UcsDcxVc
            ForEach ($UCSDcxVc in $AllUCSDcxVc)
                {
                $Item = New-Object PSObject
                
                Write-Host $UCSDcxVc.DN
                Write-Host $UCSDcxVc.SwitchID
                Write-Host $UCSDcxVc.OperState
                Write-Host $UCSDcxVc.AdminState
                Write-Host $UCSDcxVc.vNIC
                Write-Host $UCSDcxVc.LinkState
                $Item | Add-Member -type NoteProperty -Name DCxVc_DN -Value $UCSDcxVc.DN
                $Item | Add-Member -type NoteProperty -Name DCxVc_SwitchID -Value $UCSDcxVc.SwitchID
                $Item | Add-Member -type NoteProperty -Name DCxVc_OperState -Value $UCSDcxVc.OperState
                $Item | Add-Member -type NoteProperty -Name DCxVc_AdminState -Value $UCSDcxVc.AdminState
                $Item | Add-Member -type NoteProperty -Name DCxVc_vNIC -Value $UCSDcxVc.vNIC
                $Item | Add-Member -type NoteProperty -Name DCxVc_LinkState -Value $UCSDcxVc.LinkState
                $AllUCSPaths += $Item + $Item1
                
                }

            }
        }

    }

$AllUCSPaths | Export-CSV -Path "AllUCSServerPaths.csv" -NoTypeInformation -UseCulture

```

The aim is to create a new “row” for each UcsDcxV object property so it can be manipulated in XL. However I’m having problems combining the 2 collections into one at the line “$AllUCSPaths += $Item + $Item1” with the error:

> Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op\_Addition’.  
> At C:\Users\jmilano\Documents\PowerShell\_Scripts\UCS\Get-AllServerNICStatus.ps1:50 char:17
> 
> - 
> 
> ```
> $AllUCSPaths += $Item + $Item1
> 
> ```
> 
> - 
> 
> ```
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> ```
> 
> - CategoryInfo : InvalidOperation: (op\_Addition:String) , RuntimeException
> - FullyQualifiedErrorId : MethodNotFound

Can anyone suggest a good way to fix this?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [July 28, 2023, 1:00pm UTC](https://forums.powershell.org/t/combine-2-collections-into-one-for-csv-file-export/22671/2 "2023-07-28T13:00:39Z")

</div>

The problem is this: `$Item + $Item1`

If you want `$Item1` to be a property of `$Item` then you need something like this:

```auto
$Item | Add-Member -MemberType NoteProperty -Name 'Item1' -Value $Item1

```

You might want to look at the `[PSCustomObject]` type accelerator for object creation, not least for readability:

```auto
    $Item1 = [PSCustomObject] @{
        UCSServer_AdminState = $UCSServer.AdminState
        UCSServer_AssignToDN = $UCSServer.AssignedToDN
        UCSServer_Associateion = $UCSServer.Association
        UCSServer_Availability = $UCSServer.Availability
        UCSServer_ChassisID = $UCSServer.ChassisID
        UCSServer_Discovery = $UCSServer.Discovery
        UCSServer_DiscoverStatus = $UCSServer.DiscoverStatus
        UCSServer_Model = $UCSServer.Model
        UCSServer_Name = $UCSServer.Name
        UCSServer_NumOfAdapters = $UCSServer.NumOfAdapters
        UCSServer_OperPower = $UCSServer.OperPower
        UCSServer_OperState = $UCSServer.OperState
        UCSServer_Operability = $UCSServer.Operability
        UCSServer_Serial = $UCSServer.Serial
        UCSServer_ServerID = $UCSServer.ServerID
        UCSServer_SLotID = $UCSServer.SLotID
        UCSServer_UsrLbl = $UCSServer.UsrLbl
        UCSServer_Vendor = $UCSServer.Vendor
        UCSServer_DN = $UCSServer.DN
        UCSServer_RN = $UCSServer.RN
    }

```

`+=` for adding elements to an array is considered a bad practice, and can cause performance problems with large collections.

This would be better:

```auto
$AllUCSPaths = ForEach ($UCSServer in $AllUCSServers) { ... }

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/combine-2-collections-into-one-for-csv-file-export/22671/3 "2024-05-16T20:14:45Z")

</div>


