Skip to content

-NoTypeInformation should be default on Export-Csv #5131

Closed

Description

In most situations, the default behavior of Export-Csv is undesirable. Its usage as an object Import and Export is limited to begin with. What it is more commonly used for (in my experience, anyway) is to produce CSV files either to be parsed by other automated processes outside the PowerShell ecosystem or to be used in Excel as human parsable reports. The initial line with the type information is undesirable in those situations.

This is a common "gotcha" for PowerShell novices. It violates the Principle of least astonishment.

I'm curious how many profiles have this $PSDefaultParameterValues['Export-CSV:NoTypeInformation'] = $true. I know I have that in my work profile and at the top of so many scripts.

It's a minor annoyance and a breaking change, but I just don't think the default behavior is actually useful.

My tentative proposal is to leave -NoTypeInformation in for backwards compatibility but make it default and add a -IncludeTypeInformation switch to revert to legacy behavior per request.

This issue is for discussion, yays, nays, and possible alternatives for a proposed solution.

Activity

  1. added
    Breaking-Change
    Issue-Discussion
    Issue-Enhancement
    on Oct 16, 2017
  2. iSazonov commented on Oct 16, 2017

    @iSazonov
    Collaborator

    Do anybody use the type information?

  3. ww40 commented on Oct 16, 2017

    @ww40

    I can't say I've had a use for the type information in my outputs when I use Export-Csv

  4. markekraus commented on Oct 16, 2017

    @markekraus
    ContributorAuthor

    Ilya (@iSazonov) I have only ever used it for academic purposes, just to see what it does. It doesn't even re-serialize objects upon import, even simple objects like a hashtable.

    Maybe it's useful to know what the object was before it was exported? But I've never personally seen it used in that manner. Usually when a user wants to export and import objects and attempt to preserve type data they will used *-Clixml cmdlets, JSON, or a custom serializer.

  5. Jawz84 commented on Oct 16, 2017

    @Jawz84
    Contributor

    Agree fully with Mark Kraus (@markekraus) here. Having -NoTypeinformation as default would be a good breaking change. ++ For -WithTypeInformation

  6. iSazonov commented on Oct 16, 2017

    @iSazonov
    Collaborator

    I believe the design was with PSObject type in mind to export/import. For other types it seems there's no point in doing that.

  7. markekraus commented on Oct 16, 2017

    @markekraus
    ContributorAuthor

    Ilya (@iSazonov) maybe, but it doesn't even preserve custom PSTypeNames:

    [PSCustomObject]@{
        PSTypeName = 'My.Custom.Object'
        Column1 = 'values1'
    } | Export-Csv C:\temp\csv.csv
    $import = Import-Csv C:\temp\csv.csv
    $import.PSObject.TypeNames
    CSV:My.Custom.Object
    

    It already pulls in as a PSCustomObject:

    $import[0].GetType().fullname
    System.Management.Automation.PSCustomObject
    

    I should note it does that for all objects exported and imported through *-Csv

    @{
        Column1 = 'values1'
    } | Export-Csv C:\temp\csv.csv
    $import = Import-Csv C:\temp\csv.csv
    $import.PSObject.TypeNames
    $import[0].GetType().fullname
    CSV:System.Collections.Hashtable
    System.Management.Automation.PSCustomObject
    

    so I'm still left wondering what the use-case is.

  8. iSazonov commented on Oct 16, 2017

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) By the way, it might make sense to fix. Could you please open new Issue?

  9. markekraus commented on Oct 16, 2017

    @markekraus
    ContributorAuthor

    Ilya (@iSazonov) Fix in which way?

  10. iSazonov commented on Oct 16, 2017

    @iSazonov
    Collaborator

    To recover correct object types in your type.

  11. markekraus commented on Oct 16, 2017

    @markekraus
    ContributorAuthor

    Ilya (@iSazonov) agreed, it might be worth making that breaking change in addition to switching the default behavior.

  12. iSazonov commented on Oct 16, 2017

    @iSazonov
    Collaborator

    I think it's an independent case.

  13. 26 remaining items

  14. markekraus commented on Oct 17, 2017

    @markekraus
    ContributorAuthor

    Ilya (@iSazonov) I could do that. ;) I think this one got traction because I advertised it on Reddit and twitter.. and.. because it is almost universally disliked feature of the language. I wanted to advertise it in the hopes that someone would provide feedback on how they actually use the default behavior. So far, I guess no one does?

    As for a label and auto-Reddit-Post, That would be something I could do. seems like the perfect job for PowerShell and PSRAW. 😄

  15. rossmeyer1 commented on Oct 17, 2017

    @rossmeyer1

    In my early experiences with PowerShell, I didn't know about -NoTypeInformation.

    I ended up including in my script something that would remove the first line of the exported CSV file to get rid of the type information!!

    I could not agree with this more.

  16. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Oct 19, 2017
  17. SteveL-MSFT commented on Oct 19, 2017

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this and agree that the default behavior should be -NoTypeInformation. However, we need to retain -NoTypeInformation to not break scripts already using it and document the change in behavior for scripts that haven't been using that switch. We should also introduce a new switch to retain previous behavior. -IncludeTypeInformation seems reasonable. Documentation and new script analyzer rules will help mitigate existing scripts that may have been skipping or removing the type information line which will now no longer work correctly.

  18. markekraus commented on Oct 19, 2017

    @markekraus
    ContributorAuthor

    For those following the thread: PR #5164

  19. lzybkr commented on Oct 19, 2017

    @lzybkr
    Contributor

    -NoTypeInformation:$false should probably be equivalent to -IncludeTypeInformation.

    And maybe the we don't need the new option, a confusing double negative is hardly obvious, but if nobody uses it, then is adding a new option helping anyone?

  20. iSazonov commented on Oct 19, 2017

    @iSazonov
    Collaborator

    We could make an enhance to restore native type.

  21. markekraus commented on Oct 19, 2017

    @markekraus
    ContributorAuthor

    Jason Shirk (@lzybkr) see #5134 may not be used now... but.. it could be improved so that it would be useful for light weight PSCustomObject Import/Export serialization.

  22. shiftybit commented on Oct 20, 2017

    @shiftybit

    I think this is a great idea. I'm constantly having to add the -NoTypeInformation switch every time I use Export-CSV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Breaking-ChangeCommittee-ReviewedIssue-DiscussionIssue-EnhancementResolution-FixedWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions