Hello Alexandru,
What you are seeing is most likely a first-access materialization/caching effect in the Active Directory PowerShell object, not a new LDAP query for each object. DistinguishedName is a core/default property on the returned ADGroup object, while extensionAttribute15 is an additional LDAP attribute requested through -Properties, so it is exposed through the AD module’s property bag and can have a noticeable first-read cost before subsequent access becomes cached in the PowerShell session. The fact that the second loop drops to almost zero strongly suggests the data is already present locally and the delay is caused by object/property resolution rather than the domain controller.
For export, I would avoid repeatedly touching the raw AD object properties and instead materialize the values once into plain PowerShell objects before exporting, for example:
Get-ADGroup -Filter "extensionAttribute15 -like 'remove'" -Server $server -Properties extensionAttribute15 |
Select-Object DistinguishedName, @{Name='extensionAttribute15';Expression={[string]$_.extensionAttribute15}} |
Export-Csv .\groups.csv -NoTypeInformation -Encoding UTF8
If this still takes an unexpectedly long time for 20,000 groups, the next useful comparison would be measuring the full Get-ADGroup query separately from the Export-Csv step, because the slow part may be the AD query, object materialization, console output, or CSV serialization rather than the property value itself.