Question

I am currently writing a new line to a CSV-file each time I successfully/unsuccessfully execute a command in my script with the export-csv command. The resulting file is a logfile.

I would like to store all of this output into some kind of variable and write it all in one go at the end of the script.. This sounds far more efficient and clever as I am currently opening/writing/closing my csv file hundreds of time on each run..

But how would I do this? Any help is greatly appreciated as always!!

Was it helpful?

Solution

Seems easy enough. Just create an empty array to hold your log lines before you start:

$log = @()

Then instead of adding to the csv file, add to the array:

$log += "whatever your log entry looks like"

If you're doing that a whole bunch of times, an arraylist might be better than an array:

$log = new-object collections.arraylist

then:

$log.add("whatever your log entry looks like")

Then at the end (for either one):

$log | add-content <path to csv file>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top