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

Published 05 May 09 04:11 PM | gbordier 

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

$a=@()

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 | % {

      $stream.WriteLine($s)

}

$stream.close()

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:

 Method

Time to completion

‘>>’

29 s

Outfile and [Array]

27 s

export-csv

22 s

StreamWriter

1.5 s

 

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

Cheers,

Next, check that with Powerhell v2!

Guillaume

 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Microsoft Services France Unified Communication Team said on May 5, 2009 10:13 AM:

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

# dak said on May 6, 2009 6:09 PM:

1 s with old school language : vbscript ;-)

# Nicolas R said on September 9, 2009 8:33 AM:

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

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About gbordier

I've been a Consultant with Microsoft Services France for 8 years in the IT field. .

Search

This Blog

Syndication

Page view tracker