Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:59:15+00:00 2026-05-13T13:59:15+00:00

I am using Powershell for some ETL work, reading compressed text files in and

  • 0

I am using Powershell for some ETL work, reading compressed text files in and splitting them out depending on the first three characters of each line.

If I were just filtering the input file, I could pipe the filtered stream to Out-File and be done with it. But I need to redirect the output to more than one destination, and as far as I know this can’t be done with a simple pipe. I’m already using a .NET streamreader to read the compressed input files, and I’m wondering if I need to use a streamwriter to write the output files as well.

The naive version looks something like this:

while (!$reader.EndOfFile) {
  $line = $reader.ReadLine();
  switch ($line.substring(0,3) {
    "001" {Add-Content "output001.txt" $line}
    "002" {Add-Content "output002.txt" $line}
    "003" {Add-Content "output003.txt" $line}
    }
  }

That just looks like bad news: finding, opening, writing and closing a file once per row. The input files are huge 500MB+ monsters.

Is there an idiomatic way to handle this efficiently w/ Powershell constructs, or should I turn to the .NET streamwriter?

Are there methods of a (New-Item “path” -type “file”) object I could use for this?

EDIT for context:

I’m using the DotNetZip library to read ZIP files as streams; thus streamreader rather than Get-Content/gc. Sample code:

[System.Reflection.Assembly]::LoadFrom("\Path\To\Ionic.Zip.dll") 
$zipfile = [Ionic.Zip.ZipFile]::Read("\Path\To\File.zip")

foreach ($entry in $zipfile) {
  $reader = new-object system.io.streamreader $entry.OpenReader();
  while (!$reader.EndOfFile) {
    $line = $reader.ReadLine();
    #do something here
  }
}

I should probably Dispose() of both the $zipfile and $reader, but that is for another question!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:59:15+00:00Added an answer on May 13, 2026 at 1:59 pm

    Reading

    As for reading the file and parsing, I would go with switch statement:

    switch -file c:\temp\stackoverflow.testfile2.txt -regex {
      "^001" {Add-Content c:\temp\stackoverflow.testfile.001.txt $_}
      "^002" {Add-Content c:\temp\stackoverflow.testfile.002.txt $_}
      "^003" {Add-Content c:\temp\stackoverflow.testfile.003.txt $_}
    }
    

    I think it is better approach because

    • there is support for regex, you don’t
      have to make substring (which might
      be expensive) and
    • the parameter
      -file is quite handy 😉

    Writing

    As for writing the output, I’ll test to use streamwriter, however if performance of Add-Content is decent for you, I would stick to it.

    Added:
    Keith proposed to use >> operator, however, it seems that it is very slow. Besides that it writes output in Unicode which doubles the file size.

    Look at my test:

    [1]: (measure-command {
    >>     gc c:\temp\stackoverflow.testfile2.txt  | %{$c = $_; switch ($_.Substring(0,3)) {
    >>             '001'{$c >> c:\temp\stackoverflow.testfile.001.txt} `
    >>             '002'{$c >> c:\temp\stackoverflow.testfile.002.txt} `
    >>             '003'{$c >> c:\temp\stackoverflow.testfile.003.txt}}}
    >> }).TotalSeconds
    >>
    159,1585874
    [2]: (measure-command {
    >>     gc c:\temp\stackoverflow.testfile2.txt  | %{$c = $_; switch ($_.Substring(0,3)) {
    >>             '001'{$c | Add-content c:\temp\stackoverflow.testfile.001.txt} `
    >>             '002'{$c | Add-content c:\temp\stackoverflow.testfile.002.txt} `
    >>             '003'{$c | Add-content c:\temp\stackoverflow.testfile.003.txt}}}
    >> }).TotalSeconds
    >>
    9,2696923
    

    The difference is huge.

    Just for comparison:

    [3]: (measure-command {
    >>     $reader = new-object io.streamreader c:\temp\stackoverflow.testfile2.txt
    >>     while (!$reader.EndOfStream) {
    >>         $line = $reader.ReadLine();
    >>         switch ($line.substring(0,3)) {
    >>             "001" {Add-Content c:\temp\stackoverflow.testfile.001.txt $line}
    >>             "002" {Add-Content c:\temp\stackoverflow.testfile.002.txt $line}
    >>             "003" {Add-Content c:\temp\stackoverflow.testfile.003.txt $line}
    >>             }
    >>         }
    >>     $reader.close()
    >> }).TotalSeconds
    >>
    8,2454369
    [4]: (measure-command {
    >>     switch -file c:\temp\stackoverflow.testfile2.txt -regex {
    >>         "^001" {Add-Content c:\temp\stackoverflow.testfile.001.txt $_}
    >>         "^002" {Add-Content c:\temp\stackoverflow.testfile.002.txt $_}
    >>         "^003" {Add-Content c:\temp\stackoverflow.testfile.003.txt $_}
    >>     }
    >> }).TotalSeconds
    8,6755565
    

    Added: I was curious about the writing performance .. and I was a little bit surprised

    [8]: (measure-command {
    >>     $sw1 = new-object io.streamwriter c:\temp\stackoverflow.testfile.001.txt3b
    >>     $sw2 = new-object io.streamwriter c:\temp\stackoverflow.testfile.002.txt3b
    >>     $sw3 = new-object io.streamwriter c:\temp\stackoverflow.testfile.003.txt3b
    >>     switch -file c:\temp\stackoverflow.testfile2.txt -regex {
    >>         "^001" {$sw1.WriteLine($_)}
    >>         "^002" {$sw2.WriteLine($_)}
    >>         "^003" {$sw3.WriteLine($_)}
    >>     }
    >>     $sw1.Close()
    >>     $sw2.Close()
    >>     $sw3.Close()
    >>
    >> }).TotalSeconds
    >>
    0,1062315
    

    It is 80 times faster.
    Now you you have to decide – if speed is important, use StreamWriter. If code clarity is important, use Add-Content.


    Substring vs. Regex

    According to Keith Substring is 20% faster. It depends, as always. However, in my case the results are like this:

    [102]: (measure-command {
    >>     gc c:\temp\stackoverflow.testfile2.txt  | %{$c = $_; switch ($_.Substring(0,3)) {
    >>             '001'{$c | Add-content c:\temp\stackoverflow.testfile.001.s.txt} `
    >>             '002'{$c | Add-content c:\temp\stackoverflow.testfile.002.s.txt} `
    >>             '003'{$c | Add-content c:\temp\stackoverflow.testfile.003.s.txt}}}
    >> }).TotalSeconds
    >>
    9,0654496
    [103]: (measure-command {
    >>     gc c:\temp\stackoverflow.testfile2.txt  | %{$c = $_; switch -regex ($_) {
    >>             '^001'{$c | Add-content c:\temp\stackoverflow.testfile.001.r.txt} `
    >>             '^002'{$c | Add-content c:\temp\stackoverflow.testfile.002.r.txt} `
    >>             '^003'{$c | Add-content c:\temp\stackoverflow.testfile.003.r.txt}}}
    >> }).TotalSeconds
    >>
    9,2563681
    

    So the difference is not important and for me, regexes are more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.