PowerShell / PowerShell/PowerShell

Add option to Export-Csv / ConvertTo-Csv for user-configurable formats

Open
#19,579 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary of the new feature / enhancement

(follow-up to https://github.com/PowerShell/PowerShell/issues/19536)

In the current implementation of Export-Csv / ConvertTo-Csv, data is simply formatted using .ToString(), which uses the current culture's formats.

This is not always desirable -- for instance, milliseconds are lost in DateTime properties --, and changing the culture's format locally in code requires a non-obvious technique involving cloning the culture.

This would be tackled by adding a -Format option to Export-Csv / ConvertTo-Csv that would support specifying alternate formats on a per-property or per-type basis:

# (derived from @mklement0's proposals in https://github.com/PowerShell/PowerShell/issues/19536)
[pscustomobject] @{
  Name = 'foo'
  Ratio = 1 / 9
  Type = 'known'
  StartDate = Get-Date
  EndDate = (Get-Date).AddDays(1)
  Precise = 1 / 7
} | ConvertTo-Csv -Format @{ [datetime] = 'yyyy-MM-dd HH:mm:ss.fff'; [double] = 'F2'; Precise = 'F10' }

This would output:

"Name","Ratio","Type","StartDate","EndDate","Precise"
"foo","0.11","known","2023-04-27 17:23:28.540","2023-04-28 17:23:28.540","0.1428571429"
Proposed technical implementation details (optional)

No response

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 reviewing the Export-Csv and ConvertTo-Csv cmdlets and the related discussion in issue 19536. Use the examples in this issue to clarify the proposed per-property and per-type -Format behavior, including DateTime precision and numeric formats. Done means the option's design and expected output are defined and covered for both cmdlets.

Written by the indexing model from the issue text.

Assessment

Tech stack
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.