Powershell and writing files (how fast can you write to a file? )

Hi there,

I’ve been working for some time on a tool similar to PAL from mike lagase in order to automate the analysis of loadgen runs.

While doing this I had to write large files with PowerShell and was not impressed with the result.

I thought I’d share the problem and solution with the community.

There are at least four ways of writing files with PSH:

·         use the ‘>>’' alias that calls into the out-file cmd-let

·         use export-csv

·         use .Net

So let’s say I want to write 10000 lines into a file.

Method 1: use the >> operator

$s=”some text”

1..10000 | % {
     $s >> ".\test.txt"


This way you actually open and close the file 1000 times.

Method 2: use out-file to write an Array

1..10000 | % {
    $a+=[Array] $s

$a | out-file ".\test.txt"

This way actually writes once, using powershell.

Method 3: use export-csv to write an Array


1..10000 | % {

      $a+=[Array] $s


$a | export-csv "t.txt"

Export-csv can also write the array for you


Method 4: use .Net StreamWriter to write the file


$stream = [System.IO.StreamWriter] "t.txt"

1..10000 | % {




The StreamWriter object from .Net also does the work nicely.

Conclusion: how fast ?

I tried all methods on my laptop, and here is what I got:


Time to completion


29 s

Outfile and [Array]

27 s


22 s


1.5 s


Well results speak by themselves, if you are in a hurry, use .Net StreamWriter !


Next, check that with Powerhell v2!



Comments (20)

  1. Ian Bruckner says:

    $b = 1..10000

    $b | Out-File C:Temptemp.txt

    2.3 seconds

  2. Anonymous says:

    Je viens de publier un petit post rapide sur la méthode d’écriture dans un fichier avec PSH : Powershell

  3. Anonymous says:

    Thank you for this. I’m using this in a function to continuously write the output from a serial port to a file

  4. Dak says:

    1 s with old school language : vbscript 😉

  5. Nicolas R says:

    Merci pour ce post, interessant surtout avec le temps d’execution, A+

  6. @dak

    > file.txt doesn't accomplish the same thing

  7. Martin says:

    Very interesting. However, you compared a little different between these methods. You use  the StreamWriter to write line by line, and you use the other methods to write in one big chunk. Maybe this affects performance? I made a comparison myself (using the big chunk method), and the >> method and StreamWriter method had the same performance (both 40 seconds).

  8. Marcus says:

    In your example for Stream Writer it does not output anything to the text file…

    You should have $_ in place of your $s


    $stream = [System.IO.StreamWriter] "t.txt"

    1..10000 | % {




  9. @Marcus — The four snippets actually belong in one file, that is why you see reuse of the $s variable in all 4 examples with only one definition in the first.  It would probably have been better to post the 4 examples as a single file, or post 4 complete files with all definitions needed included in each one.

  10. BRiddle says:

    Method 3 doesn't actually work for an array; at least it didn't for mine.  Export-CSV looks at properties and values, not array contents.    So as written above, it will write a file out, but when run against an array of 16 character addresses, it will produce a single column with header value of "Length" in row zero and with the "16" length of each entry on every other output line.

    To make that work, more code has to be added.  I've seen solutions where the array is piped into SELECT-OBJECT creating a new PSObject for each value before the export.  I've seen other solutions that claim adding " -Delimiter ";" -NoTypeInformation" to the Export will help, but haven't tested it since methods 1 and 2 already worked fine for me.



  11. Anonymous says:

    Pingback from Powershell | Pearltrees

  12. BatchGameFiles says:

    only 0.08 sec with the funny language Batch

  13. cornasdf says:

    Thanks for this. I was able to take a 500k line out-file -append script and cut it down from well over an hour to about 18 minutes using the streamWriter suggestion.

  14. stef says:

    Thank you, I was looking for a simple means of generating output (wasn’t remembering out-file) and found something even better.

  15. Brienna says:

    I’ve been looking everywhere for this! Thank goodness I found it on Google. You have made my day!


  16. vikram says:

    thanks Streamwriter solved my issue for writing large dataset into text file, it’s quite efficient and file size also is very compact

  17. http://goo.gl/cMsfZs">obat hiv says:

    HIV ialah suatu jenis virus yang mengakibatkan sakit HIV. Virus AIDS kebiasaannya menyerang seseorang serta menyerang sistem imunitas seseorang, yang akhirnya mengakibatkan tubuh jadi lemah dalam melawan infeksi. Setiap orang yang terkena HIV kebiasaannya
    kekurangan sistem imun dalam tubuh.

  18. PowershellAdministrator says:

    Thanks a lot for this and pointing me in this direction. There are two flaws in your test though. A little one: The export-csv option will give a file with 1 line and the lenght of the string. I also missed the .net io.file class in your tests. The big
    flaw: I saw that you use the Foreach-Object ( % is an alias for this) loop, which is the slowest loop around. It would be better to use the ForEach loop or For loop. Based on your tests, I decided to create a test script that tests all the writing methods
    I know of. And since the loops also can have different running times, I decided to test this as well. If you want you can find the test here (including a test script to run the tests yourself on your own computer)

    https://powershelladministrator.wordpress.com/2015/11/15/speed-of-loops-and-different-ways-of-writing-to-files-which-is-the-quickest/ . I also linked your page in my article, since you are one of the sources that put me on this track

  19. Gideon says:

    I googled this while I was inpatient, Streamwriter rocks! Thanks:)

  20. PowerShellNewbie says:

    Hi Guillaume,

    thank you so much for your helpful post. Is it possible to use the ‘.Net StreamWrite’ Option in a pipe?

    E.g. something like:

    findstr /V "##" .my_file.txt | $stream.WriteLine

    Thanks so much!

Skip to main content