If your array has objects as you say like
$array1 = [PsCustomObject]@{Id=1; Count=24},
[PsCustomObject]@{Id=2; Count=34}
$array2 = [PsCustomObject]@{Id=1; Name="Some name"},
[PsCustomObject]@{Id=2; Name="Some other name"}
You can do something like this:
# this updates the objects in $array1
foreach ($item in $array1) {
foreach ($obj in $item) {
$obj | Add-Member -MemberType NoteProperty -Name 'Name' -Value ($array2 | Where-Object { $_.Id -eq $obj.Id }).Name
}
}
# show on screen
$array1
# export to CSV
$array1 | Export-Csv -Path 'D:\Test\Joined.Csv' -NoTypeInformation
Output on screen:
Id Count Name
-- ----- ----
1 24 Some name
2 34 Some other name
If the arrays store Hashtables like
$array1 = @{Id=1; Count=24},
@{Id=2; Count=34}
$array2 = @{Id=1; Name="Some name"},
@{Id=2; Name="Some other name"}
Then use this:
# this returns a new array of PSObjects
$result = foreach ($item in $array1) {
foreach ($hash in $item) {
$hash['Name'] = ($array2 | Where-Object { $_.Id -eq $hash.Id }).Name
}
[PsCustomObject]$hash
}
# show on screen
$result
# export to CSV
$result | Export-Csv -Path 'D:\Test\JoinedFromHashes.Csv' -NoTypeInformation
Output on screen:
Name Id Count
---- -- -----
Some name 1 24
Some other name 2 34
Note that Hashtables are unordered by default, so the order in which the columns appear may vary