Array to multiple CSV - Performance improvement?

I have the following code abstract:

[pscustomobject]$resultSet = Get-ADObject ...
$users = [System.Collections.ArrayList]@()
$groups = [System.Collections.ArrayList]@()
foreach ($result in $resultSet)
{
    if ($result.objectClass -eq "group") {
        $null = $groups.Add([pscustomobject]@{
            ...
    }
    if ($result.objectClass -eq "user") {
        $null = $users.Add([pscustomobject]@{
            ...
    }
}
$groups | Export-Csv ...
$users | Export-Csv ...

Is there a way to improve the performance of the export to individual CSV files?

The Get-ADObject bit takes about 13 seconds (23000 entries), it takes another 5 seconds to build the individual arrays but then takes another 5 minutes to output to the CSV files. I need the output to be split into different files as it’ll be consumed by downstream systems but I’m surprised at how long it’s taking.

Any advice on improving the Export-Csv portion of the code?

Thanks.

Found the solution:

[pscustomobject]$resultSet = Get-ADObject ...
[System.IO.StreamWriter]$usersFile = [System.IO.StreamWriter]::new(...
[System.IO.StreamWriter]$groupsFile = [System.IO.StreamWriter]::new(...
foreach ($result in $resultSet)
{
    if ($result.objectClass -eq "group") {
        $groupsFile.WriteLine(('"{0}","{1}","{2}","{3}","{4}","{5}"' -f $result.objectGUID, ...
    }
    if ($result.objectClass -eq "user") {
        $usersFile.WriteLine(('"{0}","{1}","{2}","{3}","{4}","{5}"' -f $result.objectGUID, ...
    }
}
$usersFile.Close()
$groupsFile.Close()

This has reduced the write time to 23 seconds! I’ve added code for the header row, etc.

I don’t know how many objects you export but even with a decent amount of elements it should take only seconds. Do you write the CSV files lokally or to a network share?