Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -NoHeader parameter to ConvertTo-Csv and Export-Csv cmdlets #19108

Merged
merged 8 commits into from Feb 8, 2023

Conversation

ArmaanMcleod
Copy link
Contributor

@ArmaanMcleod ArmaanMcleod commented Feb 5, 2023

PR Summary

Fixes #17527

Added -NoHeader to ConvertTo-Csv and Export-Csv cmdlets.

PR Context

Currently we have no easy way to export a CSV file without headers, but we have the ability to import CSV files with headers using Import-Csv -Header.

This change allows ConvertTo-Csv and Export-Csv to export without headers. This simplifies scenarios where the user may not care about headers and removes need to manipulate the CSV to achieve a header less CSV.

So instead of always having to skip the first row when writing to CSV:

PS C:\> $myObjects = @(
>     [PSCustomObject]@{
>         Name = 'John'
>         LastName = 'Smith'
>     }
>     [PSCustomObject]@{
>         Name = 'Freddy'
>         LastName = 'Kruger'
>     }
> )
PS C:\> $myObjects | ConvertTo-Csv | Select-Object -Skip 1 | Out-File -FilePath 'output.csv'
PS C:\> cat 'output.csv'
"John","Smith"
"Freddy","Kruger"

You can now convert to CSV without headers using ConvertTo-Csv -NoHeader:

PS C:\> $myObjects | ConvertTo-Csv -NoHeader | Out-File -FilePath 'output.csv'
PS C:\> cat 'output.csv'
"John","Smith"
"Freddy","Kruger"

You can also choose to export without headers using Export-Csv -NoHeader:

PS C:\> $myObjects | Export-Csv -Path 'output.csv' -NoHeader
PS C:\> cat 'output.csv'
"John","Smith"
"Freddy","Kruger"

Or import first, manipulate pipeline object to something else, then export again with no headers:

PS C:\> (Import-Csv -Path 'output.csv' | Select-Object -Property Name) | Export-Csv -Path 'output.csv' -NoHeader
PS C:\> cat 'output.csv'
"John"
"Freddy"

PR Checklist

@ArmaanMcleod ArmaanMcleod marked this pull request as ready for review February 5, 2023 08:23
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 5, 2023
Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor comments.

@pull-request-quantifier
Copy link

This PR has 29 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +23 -6
Percentile : 11.6%

Total files changed: 3

Change summary by file extension:
.cs : +9 -6
.ps1 : +14 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  👌  👎 (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov
Copy link
Collaborator

iSazonov commented Feb 8, 2023

@ArmaanMcleod Please open new issue in Docs repository and add reference in the PR description (where Documentation needed).

@ArmaanMcleod
Copy link
Contributor Author

@ArmaanMcleod Please open new issue in Docs repository and add reference in the PR description (where Documentation needed).

@iSazonov Done 🙂

@iSazonov iSazonov assigned iSazonov and unassigned TravisEz13 Feb 8, 2023
@iSazonov iSazonov merged commit b7e2913 into PowerShell:master Feb 8, 2023
40 checks passed
@iSazonov
Copy link
Collaborator

iSazonov commented Feb 8, 2023

@ArmaanMcleod Thanks for your contribution!

@ArmaanMcleod ArmaanMcleod deleted the export-csv-no-header branch February 8, 2023 12:51
@msftbot
Copy link

msftbot bot commented Mar 14, 2023

🎉v7.4.0-preview.2 has been released which incorporates this pull request.🎉

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add -NoHeader switch to Export-Csv and ConvertTo-Csv
4 participants