Array of objects are created dynamically with different key value pair. e.g.
1. array1 = [{"Name": "abc", "Age":"20"}]
Array 2 will be formed dynamically with different set of data
2. array2 = [{"Address":"ABC", "City":"Ahmedabad", "State":"Gujarat"}]


Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 29, 2024, 2:45 AM EST
Hi Jalpa Dhola,
In PowerShell, you can dynamically add two arrays of objects by utilizing the
ForEach-Objectcmdlet to iterate through the elements of the arrays and create a new array that combines the properties of both.# Define the first array of objects $array1 = @( [PSCustomObject]@{ Name = "abc"; Age = "20" } ) # Define the second array of objects $array2 = @( [PSCustomObject]@{ Address = "ABC"; City = "Ahmedabad"; State = "Gujarat" } ) # Combine the two arrays into a new array $combinedArray = @() ForEach ($item1 in $array1) { ForEach ($item2 in $array2) { $combinedObject = [PSCustomObject]@{ Name = $item1.Name Age = $item1.Age Address = $item2.Address City = $item2.City State = $item2.State } $combinedArray += $combinedObject } } # Output the combined array $combinedArray