Does this fix your script?
$log = @()
"OU=RDS Funcional,DC=esofitec,DC=loc", "OU=VDI Funcional,DC=esofitec,DC=loc" |
ForEach-Object {
$baseou = $_
Get-ADOrganizationalUnit -Filter * -SearchBase $_ |
ForEach-Object {
if ($_.distinguishedname -ne $baseou){
$log += "Processsing {0}" -f $_.distinguishedname
[array] $users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname -SearchScope OneLevel
if ($users -ne $null) {
$log += "{0} has {1}" -f $_.name, $users.count
[PSCustomObject]@{
OU = $_.name
Users = $users.count
}
}
}
}
} | Export-Csv -Path c:\\ExportUsers.csv -NoTypeInformation -Delimiter ','
$log
EDIT: Move Export-CSV cmdlet.