Powershell: Add objects to an array of objects

To append to an array, just use the += operator.

$Target += $TargetObject

Also, you need to declare $Target = @() before your loop because otherwise, it will empty the array every loop.


Typically people do this without using the inefficient "+=" that makes a new copy of the array every time:

$Target = foreach ($Machine in $Machines)
{
  $TargetProperties = @{Name=$Machine}  
  $TargetObject = New-Object PSObject –Property $TargetProperties
}