The I wrote the below script to return the name of all the Organisational Units in the domain that are empty.
I would like the script to also tell me the total number of empty OU's at the end, by counting the lines returned. I've tried assigning an $array to the New-Object, and adding various versions of $array.count or | Measure-Object at the end of the script, but all return 0.
Get-ADOrganizationalUnit -Filter * |
foreach {
$o = Get-ADObject -filter * -searchbase $_.distinguishedname -searchscope 1
$total = ($o | Measure-Object).Count
New-Object psobject -Property @{
Name=$_.distinguishedname
} |
where-object {$total -le "0"}
}