PowerShell / PowerShell/PowerShell

`Export-Csv`/`ConvertTo-Csv` to better stringify complex properties

Open
#20,383 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue-Enhancement Needs-Triage
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Summary of the new feature / enhancement

Although questionable, trying to put a structured object into a flat csv file, is a quite common question at stackoverflow (see e.g.: Getting System.Object[] issue for JSON Array data in output in PowerShell Script). This is often based on a misconception of the concerned structures, stingifying it to just the type name (as e.g. System.Object[]) is quiet pointless, instead I would expect the complex properties to have a similar value as the display format of Format-Table:

$List = [PSCustomObject]@{
  int = 1
  string = 'One'
  Array = 1, 2
  HashTable = @{ '1' = 'One'; '2' = 'Two' } # Using string keys, see: #5749
  Object = [PSCustomObject]@{ 1 = 'One'; 2 = 'Two' }
},
[PSCustomObject]@{
  int = 2
  string = 'Two'
  Array = 3, 4
  HashTable = @{ '3' = 'Three'; '4' = 'Four' }
  Object = [PSCustomObject]@{ 3 = 'Three'; 4 = 'Four' }
}

(2023-10-01: Changed hashtable to string keys for reason explained at: #5749)

Format-Table output
$List | Format-Table

int string Array  HashTable               Object
--- ------ -----  ---------               ------
  1 One    {1, 2} {[2, Two], [1, One]}    @{1=One; 2=Two}
  2 Two    {3, 4} {[4, Four], [3, Three]} @{3=Three; 4=Four}
Current ConvertTo-Csv output
$List | ConvertTo-Csv
"int","string","Array","HashTable","Object"
"1","One","System.Object[]","System.Collections.Hashtable","@{1=One; 2=Two}"
"2","Two","System.Object[]","System.Collections.Hashtable","@{3=Three; 4=Four}"
Expected ConvertTo-Csv output (wishful thinking)
$List | ConvertTo-Csv
"int","string","Array","HashTable","Object"
"1","One","{1, 2}","{[2, Two], [1, One]}","@{1=One; 2=Two}"
"2","Two","{3, 4}","{[4, Four], [3, Three]}","@{3=Three; 4=Four}"

Apart from the fact that is makes more sense, it might also help to troubleshoot and understand the situation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Export-Csv and ConvertTo-Csv entry points and their existing tests, then compare current complex-property stringification with the Format-Table examples in the issue. Define and test the intended representations for arrays, hash tables, and nested objects while preserving existing scalar output; done means the expected CSV examples are covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.