Skip to content

Import-Csv -ReadCount #25736

Closed
Closed

Description

Summary of the new feature / enhancement

As a user I want to incrementally read a large log.csv to perform processing and organisation, in order to avoid running out of memory.

Proposed technical implementation details (optional)

No response

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Jul 7, 2025
  2. JamesDownes5 commented on Jul 7, 2025

    @JamesDownes5
    Author

    I am happy to implement this, I just need some direction and showing where the current Import-Csv implementation is

  3. MartinGC94 commented on Jul 7, 2025

    @MartinGC94
    Contributor

    Are you sure it doesn't already stream things if you use the pipeline? Import-Csv | foreach {# Do something

  4. mklement0 commented on Jul 7, 2025

    @mklement0
    Contributor

    Indeed, MartinGC94, it streams, yet it's still possible to run out of memory.

    Related:

    James Downes (@JamesDownes5) , were you thinking that batching the reading with -ReadCount would allow you to force garbage-collection after every N objects in order to relieve the memory pressure?

  5. JamesDownes5 commented on Jul 7, 2025

    @JamesDownes5
    Author

    This is what I currently have:

    $hash = Import-Csv .\IMPROTANT_DATA.csv | Group-Object ID -AsHashTable
    foreach ($key in $hash.keys) {$hash[$key] | Export-Csv -NoTypeInformation -UseQuotes Never -Path ".\$key.csv"}
    

    MartinGC94 How would I go about making this a complete pipeline?

    This is my attempt at batching:

    $hash = @{}
    
    Get-Content .\IMPROTANT_DATA.csv -ReadCount 512 | 
    ForEach-Object {
        $ID,$Name,$Information = ($_ -split ',')
        if ($hash.ContainsKey($ID)) {
            $hash[$ID].Add([ordered]@{ ID=$ID; Name=$Name; Information=$Information; })
        } else {
            $hash[$ID] = [System.Collections.Generic.List[System.Collections.Hashtable]]::new()
            $hash[$ID].Add([ordered]@{ ID=$ID; Name=$Name; Information=$Information; })
        }
    }
    
    foreach ($key in $hash.keys) {$hash[$key] | Export-Csv -NoTypeInformation -UseQuotes Never -Append -Path ".\$key.csv"}
    

    The desired process is: Read Chunk of Input File > Convert to Hashtable based on First Column > Write/Append Data to File > Repeat until End of Input File

    The data doesn't even need to be turned into a PSObject, could just be left as Strings.

  6. MartinGC94 commented on Jul 7, 2025

    @MartinGC94
    Contributor

    You could do it like this:

    Import-Csv .\IMPROTANT_DATA.csv | group ID | foreach {
        $_.Group | Export-Csv -NoTypeInformation -UseQuotes Never -Path ".\$($_.Name).csv"
    }
    

    But the issue is that you are using Group-Object which would need to have all the data in memory to group elements so you won't gain anything by doing this.

  7. mklement0 commented on Jul 7, 2025

    @mklement0
    Contributor

    If runtime performance isn't a concern, you can avoid Group-Object as follows, which opens and closes each output file for every object written to it:

    # Remove any preexisting output CSVs.
    Remove-Item *.csv -WhatIf
    
    Import-Csv .\IMPORTANT_DATA.csv |
      ForEach-Object { 
        $_ | Export-Csv -NoTypeInformation -UseQuotes Never -Append -LiteralPath ".\$($_.Id).csv"
      }

    If you still run out of memory, try calling [GC]::Collect(); [GC]::WaitForPendingFinalizers() after every N objects.

  8. iRon7 commented on Jul 8, 2025

    @iRon7

    You might use the steppable pipeline for this and directly stream the results into multiple output pipelines.
    For a full explanation see: https://devblogs.microsoft.com/powershell-community/mastering-the-steppable-pipeline/#multiple-output-pipelines

    $Pipeline = @{}
    Import-Csv .\IMPORTANT_DATA.csv |
        ForEach-Object -Process {
            if (!$Pipeline.Contains($_.Id)) {
                $Pipeline[$_.Id] = { Export-CSV -noType -Path .\$($_.Id).csv }.GetSteppablePipeline()
                $Pipeline[$_.Id].Begin($True)
            }
            $Pipeline[$_.Id].Process($_)
        } -End {
            foreach ($Key in $Pipeline.Keys) { $Pipeline[$Key].End() }
        }

    See also: #20123 Tee-Object -Process

    PowerShell Community
    The PowerShell pipeline explained from the beginning to the end.
  9. added
    WG-NeedsReview
    WG-Cmdlets
    on Sep 11, 2025
  10. moved this from Queue to In-Progress-Issues in Cmdlets Working Groupon Nov 19, 2025
  11. SteveL-MSFT commented on Nov 19, 2025

    @SteveL-MSFT
    Member

    The @PowerShell/wg-powershell-cmdlets discussed this. We believe the right way to address the memory issue due to PSNoteProperty and the garbage collector is to enable -AsHashTable switch for both ConvertFrom-Csv and Import-Csv as described in #11027. We will close this issue as a dupe of that one and reactive the linked issue.

  12. moved this from In-Progress-Issues to Reviewed in Cmdlets Working Groupon Nov 19, 2025
  13. added
    Resolution-Duplicate
    WG-Reviewed
    and removed
    Needs-Triage
    WG-NeedsReview
    on Nov 19, 2025
  14. microsoft-github-policy-service commented on Nov 20, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.

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-EnhancementResolution-DuplicateWG-CmdletsWG-Reviewed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions