list empty OU's

hi experts,

i’m using below query to list empty OU’s fom AD. but its not working :frowning:

could some one help me ?

Get-ADObject -Filter {objectclass -eq “OrganizationalUnit”} | Where-Object {@(get-adobject -Filter * -SearchBase $_.DistinguishedName).count -eq 1} | Out-File d:\ou.txt

When i ran it i received an error on the first cmdlet. Having looked at the examples in get-help i saw that he error is in how you passed the information to the filter parameter. I changed your code to this:

Get-ADObject -Filter ‘objectclass -eq “organizationalunit”’ | Where-Object {@(get-adobject -Filter * -SearchBase $_.DistinguishedName).count -eq 1}

and it works nicely. The other alternative is to replace the first cmdlet with Get-ADOrganizationalUnit which saves a lot of typing and is nice and simple.

Here’s an archived thread about the same topic from a while back:
https://powershell.org/forums/topic/how-to-get-list-of-empty-ou/

At a glance, I’m curious why they are checking to see if the count is greater than 2, but I can’t test this right now. Perhaps you should review that script and try replacing your -eq 1 with -le 2 and see what results you get back.