0

I’m trying to create a report in Powershell but running into a problem when trying to export. Basically I have a loop that is adding the data to an array, however when I go to export all the data isn’t exporting. I broke it out of the loop so I can show you what’s happening.

What I'm expecting:

+----------+-----------+--------+
| HostName | Platform  | 3rdCol |
+----------+-----------+--------+
|  Test1   | Platform1 |        |
|  Test2   | Platform2 | 3rdCol |
+----------+-----------+--------+

What I'm getting:

+----------+-----------+
| HostName | Platform  |
+----------+-----------+
|  Test1   | Platform1 |
|  Test2   | Platform2 |
+----------+-----------+

Powershell:

#Create report array
$report = @()

#New object for Test1
$stats = New-object PSCustomObject
#Hostname column for Test1
$stats | Add-Member -Name HostName -Value Test1 -Membertype NoteProperty
#Platform column for Test1
$stats | Add-Member -Name Platform -Value Platform1 -Membertype NoteProperty
#Add row to report
$report += $stats

#New object for Test2
$stats = New-object PSCustomObject
#Hostname column for Test2
$stats | Add-Member -Name HostName -Value Test2 -Membertype NoteProperty
#Platform column for Test2
$stats | Add-Member -Name Platform -Value Platform2 -Membertype NoteProperty
#3rd column for Test2
$stats | Add-Member -Name 3rdCol -Value 3rdCol -Membertype NoteProperty
#Add row to report
$report += $stats

#Export only has two columns instead of 3
$report | export-csv c:\temp\report.csv -notypeinformation
3
  • Export-Csv infers the column headers from the first object that is piped to it. Instead of only adding the 3rdCol property to some objects, add it to all objects (just set it to $null for objects that don't need it) Commented Feb 10, 2016 at 17:47
  • @MathiasR.Jessen that would work in this example, but in a loop I'm dynamically creating the columns and don't really know how many columns there will be. Commented Feb 10, 2016 at 17:51
  • Could you show us the actual script your working with? Commented Feb 10, 2016 at 17:52

1 Answer 1

2

When PS outputs an array it looks at the first record to determine what columns are needed, so you need to make sure that your first record has all of the properties of any of the members in the array, even if the value is $null.

If you do not know what the properties will be you can get that programatically after the fact with something like:

$Props=$report | ForEach{$_.PSObject.Properties} | Select -Expand Name -Unique
$Props|Where{$_ -notin $Report[0].psobject.properties.name}|ForEach{Add-Member -InputObject $Report[0] -NotePropertyName $_ -NotePropertyValue $null}
Sign up to request clarification or add additional context in comments.

3 Comments

that would work in this example, but in a loop I'm dynamically creating the columns and don't really know how many columns there will be.
Updated to take care of dynamic properties.
Thank you! Works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.