Skip to content

Make Export-Csv and ConvertTo-Csv support hashtables (dictionaries) #10999

@mklement0

Description

@mklement0

Summary of the new feature/enhancement

In many contexts in PowerShell, custom objects and hashtables can conveniently be used interchangeably, such as in JSON serialization (ConvertTo-Json)

However, Export-Csv and ConvertTo-Csv currently do not support dictionaries ((ordered) hashtables, IDictionary instances) meaningfully: they serialize the dictionary itself.

Making these cmdlets serialize the key-value pairs, analogous to property-name-value pairs in [pscustomobject] input would be helpful.

# OK - custom object input
[pscustomobject] @{ prop=1 } | ConvertTo-Csv | Should -Be '"prop"', '"1"'

# Currently unsupported: hashtable input
@{ prop=1 } | ConvertTo-Csv | Should -Be '"prop"', '"1"'

The latter test fails, indicating the currently useless serialization of hashtables:

Expected @('"prop"', '"1"'), but got
@('"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"',
 '"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Collections.Hashtable","1"'

Proposed technical implementation details (optional)

Make Export-Csv and ConvertTo-Csv detect IDictionary input and serialize its key-value pairs instead of the dictionary object itself.

Metadata

Metadata

Assignees

Labels

Issue-Enhancementthe issue is more of a feature request than a bugWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions