Je viens de publier un petit post rapide sur la méthode d’écriture dans un fichier avec PSH : Powershell
Je viens de publier un petit post rapide sur la méthode d’écriture dans un fichier avec PSH : Powershell
1 s with old school language : vbscript ;-)
Merci pour ce post, interessant surtout avec le temps d'execution, A+
@dak
> file.txt doesn't accomplish the same thing
$b = 1..10000
$b | Out-File C:\Temp\temp.txt
2.3 seconds
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).
In your example for Stream Writer it does not output anything to the text file...
You should have $_ in place of your $s
Example:
$stream = [System.IO.StreamWriter] "t.txt"
1..10000 | % {
$stream.WriteLine($_)
}
$stream.close()
@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.
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.
Thanks!
-Bob
only 0.08 sec with the funny language Batch
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.
Thank you for this. I'm using this in a function to continuously write the output from a serial port to a file