Skip to content

Make ConvertFrom-Csv and Import-Csv support outputting (ordered) hashtables as an alternative to custom objects #11027

Open

Description

Summary of the new feature/enhancement

To complement the proposal in #10999, as suggested by Rain Sallow (/u/ta11ow) (@vexx32):

I think we should also consider adding a switch to ConvertFrom-Csv and Import-Csv in order to import the data -AsHashtable for symmetry.

Note: To be a viable alternative, the output objects should be ordered hashtables ([ordered] @{}, i.e. System.Collections.Specialized.OrderedDictionary), in order to preserve the input column order.

That said, note that the existing ConvertFrom-Json -AsHashtable feature actually outputs inherently unordered [hashtable] instances.

Benefits:

  • As with ConvertFrom-Json -AsHashtable, you may want output objects that are easier to manipulate in terms of subsequent removal / addition of properties (entries).

  • Ordered hashtable instances are more lightweight than [pscustomobject] instances and would enable (faster) processing of larger input datasets, in cases where [pscustomobject] output consumes inordinate amounts of memory - see Import-CSV has very high filesize-to-memory consumption ratio of around 50 #7603

Activity

  1. added
    Issue-Enhancement
    on Nov 9, 2019
  2. BrucePay commented on Nov 9, 2019

    @BrucePay
    Collaborator

    What scenario would this enable?

  3. mklement0 commented on Nov 9, 2019

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay).

    To quote from the OP:

    Ordered hashtable instances are more lightweight than [pscustomobject] instances and should suffice in many situations.

    For instance, it would enable processing of larger input datasets, in cases where [pscustomobject] output consumes inordinate amounts of memory - see #7603

  4. mklement0 commented on Nov 9, 2019

    @mklement0
    ContributorAuthor

    Another use case: As with ConvertFrom-Json -AsHashtable, you may want output objects that are easier to manipulate in terms of subsequent removal / addition of properties (entries).

  5. iSazonov commented on Nov 9, 2019

    @iSazonov
    Collaborator

    processing of larger input datasets

    I do not see why I would prefer hash tables for the scenario. Rather typed output would be more useful and power.

  6. mklement0 commented on Nov 9, 2019

    @mklement0
    ContributorAuthor

    That is a false dichotomy, Ilya (@iSazonov) - both features are worth implementing - see #8862

    And don't forget the convenience and speed of adding / removing entries that hashtables offer.

  7. iSazonov commented on Nov 9, 2019

    @iSazonov
    Collaborator

    If you look OrderedDictionary implementation you find that both "easier to manipulate" and "more lightweight than [pscustomobject]" will not bring that you expect in common.
    So we need to know exactly the scenario where it will benefit. While I do not see such a scenario.
    We could invest more in PSObject/PSCustomObject performance - I hope there is a way to make them better.

  8. mklement0 commented on Nov 9, 2019

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov)

    If you look OrderedDictionary implementation you find that both "easier to manipulate" and "more lightweight than [pscustomobject]" will not bring that you expect in common.

    I don't know what that means, but let's look at some scenarios, using the existing ConvertFrom-Json -AsHashtable implementation:

    Note:

    • ConvertFrom-Json -AsHashtable uses [hashtable] instances, not ordered hashtables, but if memory serves, the performance penalty for using the ordered variety instead is fairly small.

    • The code uses the Time-Command function to average 10 runs with 1001 JSON objects.

    # Create a JSON array with 1001 objects with 20 properties.
    $jo =  '{ "p1": 1, "p2": 1, "p3": 1, "p4": 1, "p5": 1, "p6": 1, "p7": 1, "p8": 1, "p9": 1, "p10": 1, "p11": 1, "p12": 1, "p13": 1, "p14": 1, "p15": 1, "p16": 1, "p17": 1, "p18": 1, "p19": 1, "p20": 1 }'
    $ja = "[`n" + (($jo + ",`n") * 1e3) + $jo + "`n]"
    
    # Compare [pscustomobject] vs. [hashtable] deserialization time:
    Time-Command { # Deserialize: [pscustomobject]
      $ja | ConvertFrom-Json
    }, {           # Deserialize: [hashtable]
      $ja | ConvertFrom-Json -AsHashtable
    }
    
    '---'
    
    # Compare [pscustomobject] vs. [hashtable] property-addition time:
    Time-Command { # Add property: [pscustomobject], with Select-Object
      $objects = $ja | ConvertFrom-Json
      $newObjects  = $objects | Select-Object *, @{ n='p21'; e = { 1 } }
    }, { # Add property: [pscustomobject] (loop)
      $objects = $ja | ConvertFrom-Json
      foreach ($obj in $objects) {
        $obj | Add-Member -NotePropertyName p21 -NotePropertyValue 1
      }
     }, { # Add property: [hashtable] (loop)
      $hashtables = $ja | ConvertFrom-Json -AsHashtable
      foreach ($ht in $hashtables) {
        $ht['p21'] = 1
      }
    }

    The above yields the following on my macOS 10.15.1 machine:

    Factor Secs (10-run avg.) Command                                                                                                                                        TimeSpan
    ------ ------------------ -------                                                                                                                                        --------
    1.00   0.073              # Deserialize: [hashtable]…                                                                                                                    00:00:00.…
    1.59   0.116              # Deserialize: [pscustomobject]…                                                                                                               00:00:00.…
    ---
    1.00   0.085              # Add property: [hashtable] (loop)…                                                                                                            00:00:00.…
    4.38   0.373              # Add property: [pscustomobject], with Select-Object…                                                                                          00:00:00.…
    5.59   0.477              # Add property: [pscustomobject] (loop)…                                                                                                       00:00:00.…
    

    That is:

  9. iSazonov commented on Nov 10, 2019

    @iSazonov
    Collaborator

    It is known issue that adding properties to pscustomobject is relatively slow. The demo convinces me that we should invest in the area and improve psobject performance.
    Without a real scenario where it is critically important, this does not convince me that we need one more parameter - cmdlets with many parameters are more likely talking about bad design or bad implementation.

  10. mklement0 commented on Nov 10, 2019

    @mklement0
    ContributorAuthor

    The demo convinces me that we should invest in the area and improve psobject performance.

    Again, a false dichotomy: that is well worth doing, but independently of what is being proposed here.
    (As an aside: to be on par feature-wise, we'd then need a Remove-Property cmdlet that removes properties in place, analogous to how Add-Member adds them in place).

    At a conceptual level, custom objects and hash tables can take each other's place, each with relative strengths and weaknesses, it is perfectly reasonable to support serialization / deserialization of both types:

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. added
    Resolution-No Activity
    on Nov 16, 2023
  14. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  15. added
    WG-Reviewed
    Up-for-Grabs
    and removed
    Resolution-No Activity
    on Nov 19, 2025
  16. SteveL-MSFT commented on Nov 19, 2025

    @SteveL-MSFT
    Member

    @PowerShell/wg-powershell-cmdlets agreed this is a good feature to add

  17. added
    KeepOpen
    on Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementKeepOpenUp-for-GrabsWG-CmdletsWG-Reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions