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
$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