0

I created custom object that basically stores Date and few integers in it's keys:

$data = [Ordered]@{
    "Date" = $currentdate.ToString('dd-MM-yyyy');
    "Testers" = $totalTesterCount;
    "StNoFeedback" = $tester_status.NoFeedback;
    "StNotSolved" = $tester_status.NotSolved;
    "StSolved" = $tester_status.Solved;
    "StNoIssues" = $tester_status.NoIssues;
    "OSNoFeedback" = $tester_os.NoFeedback;
    "OSW7" = $tester_os.W7;
    "OSW10" = $tester_os.W10;
    "OfficeNoFeedback" =  $tester_Office.NoFeedback;
    "OfficeO10" = $tester_Office.O10;
    "OfficeO13" = $tester_Office.O13;
    "OfficeO16" = $tester_Office.O16;
}

I need to Output it to CSV file in a way that every value is written in new column. I tried using $data | export-csv dump.csv but my CSV looks like that:

#TYPE System.Collections.Specialized.OrderedDictionary
"Count","IsReadOnly","Keys","Values","IsFixedSize","SyncRoot","IsSynchronized"
"13","False","System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection","System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection","False","System.Object","False"

Not even close to what I want to achieve. How to get something closer to:

date,testers,stnofeedback....
04-03-2016,2031,1021....

I created the object because it was supposed to be easy to export it as csv. Maybe there is entirely different, better approach? Or is my object lacking something?

1

1 Answer 1

6

You didn't create an object, you created an ordered dictionary. A dictionary can't be exported to CSV-directly as it's a single object which holds multiple key-value-entries.

([ordered]@{}).GetType()

IsPublic IsSerial Name              BaseType     
-------- -------- ----              --------     
True     True     OrderedDictionary System.Object

To export a dictionary you need to use GetEnumerator() to get the objects one by one, which would result in this CSV:

$data = [Ordered]@{
    "Date" = (get-date).ToString('dd-MM-yyyy')
    "Testers" = "Hello world"
}

$data.GetEnumerator() | ConvertTo-Csv -NoTypeInformation
"Name","Key","Value"
"Date","Date","04-03-2016"
"Testers","Testers","Hello world"

If you want a single object, cast the hashtable of properties to a PSObject using [pscustomobject]@{}.

$data = [pscustomobject]@{
    "Date" = (get-date).ToString('dd-MM-yyyy')
    "Testers" = "Hello world"
}

$data | ConvertTo-Csv -NoTypeInformation
"Date","Testers"
"04-03-2016","Hello world"

Or if you're using PS 1.0 or 2.0:

$data = New-Object -TypeName psobject -Property @{
    "Date" = (get-date).ToString('dd-MM-yyyy')
    "Testers" = "Hello world"
}

$data | ConvertTo-Csv -NoTypeInformation
"Testers","Date"
"Hello world","04-03-2016"
Sign up to request clarification or add additional context in comments.

2 Comments

Nitpick: An ordered dictionary is an object. It's just not an object that Export-Csv knows how to pick apart. (hence PSCustomObject)
Actually it's a special type of collection-object that doesn't support foreach (pipeline included) without specifically calling getenumerator(). You would have the same problem if u used it in a c# foreach-loop. :-)

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.