0

I have a script with which I'm collecting data about users like this:

$testObject = [PSCustomObject]@{
    username    =   $username
    name        =   $name
}

Next, I'm getting more info and add to testObject like this:

$testObject | Add-Member address address

At the end I'm exporting it to csv:

$testObject | Export-Csv -Path "output.csv" -NoTypeInformation

I need to change the script so the creation of the testObject is in a loop since I will run it for a list of users. How can I write this to create a new csv when the script starts, but append all new objects?

I'm trying to avoid creating an array of PSCustomObject since I'm using the script on large amount of data and I would prefer to write one object at a time to the csv.

Jordan
  • 1
  • 1
  • 1
  • Use `remove-item -Path "output.csv"` at the beginning of your script and than the `-append` switch for `export-csv`? See `help -full export-csv` – Ocaso Protal May 30 '18 at 11:09
  • Appending in a loop usually isn't the best of options. Use a pipeline and `ForEach-Object` rather than a regular loop. For further help you need to show more of your code. – Ansgar Wiechers May 30 '18 at 17:59

0 Answers0