Is it possible to output an array of Hash Tables that have different keys?
My experiment code is as follows:
$object1 = New-Object psobject -Property @{key1 = "Yep";
key3 = "Sure!"}
$object2 = New-Object psobject -Property @{key2 = "Yep";
key3 = "Sure!"}
$object3 = New-Object psobject -Property @{key1 = "Yep";
key2 = "Yep";
key3 = "Yep"}
Write-Host "Object 1 First"
$OutArray = @()
$OutArray += $object1
$OutArray += $object2
$OutArray
Write-Host "Object 2 First"
$OutArray = @()
$OutArray += $object2
$OutArray += $object1
$OutArray
Write-Host "Object 3 First"
$OutArray = @()
$OutArray += $object3
$OutArray += $object2
$OutArray += $object1
$OutArray
It seems as though they are stored in memory, for when you "Write-Host $OutArray" on either Object 1 or 2 first, you can see the keys and values.
Ultimately, I'm trying to export to CSV a list of ADUser accounts but the ADUser objects in question don't always have all the properties. It seems as though the first element of the array will set what the 'headers' are and then preclude the displaying of other keys, the first element might not have.
Any ideas?
Select-Object -Property *,'Special Attribute'But usualy even empty attributes will be outputted You might show your "real" code.Format-TableorExport-Csvit only takes the properties of the first object into consideration, so if any other objects have a property that the first object does not (not that the property has no value, but the first object does not have the property at all) those properties are stripped in the output.$OutArray" array. Instead you should release the objects immediately as they are generated. See: Write Single Records to the Pipeline (SC03) under the Strongly Encouraged Development Guidelines.