New filters on the Home Feed, take a look!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Question about | foreach
Post Body

Hi r/powershell,

I have a very large amount of object streaming in that I want to collect and export as CSV. First I collected all objects, and exported them to disk like this:

$result = 1..100 | Foreach {

    [PsCustomObject] @{
        'Id'   = $_
    }

}

$result | Export-Csv -Path out.csv -NoTypeInformation

This is quite fast, however, very memory intense. In reality there are a few 100k objects. So I thought I write them to disk immediately and don't keep them in memory:

1..100 | Foreach {

    [PsCustomObject] @{
        'Id'   = $_
    } | Export-Csv -Path out.csv -NoTypeInformation -Append

}

This works, but it's magnitudes slower than the other version.

So I thought I could somehow keep a part of the objects in memory, and write them to disk in batches. I thought I'm clever and do it like this:

$result = 1..100 | Foreach {

    [PsCustomObject] @{
        'Id'   = $_
    }

    if ($result.Count % 10 -eq 0) {

        $result | Export-Csv -Path out.csv -Append -NoTypeInformation
        $result = $null
    }

}

But $result is always $null inside the | foreach loop. Any way to get this working? Or any other way? foreach ($Item in $Collection) is not an option in my case.

Thanks

Author
Account Strength
90%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,540
Link Karma
3,347
Comment Karma
1,193
Profile updated: 4 days ago
Posts updated: 6 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 years ago