list empty OU's

This topic contains 2 replies, has 3 voices, and was last updated by  Poshoholic 5 years ago.

  • Author
  • #8644


    hi experts,

    i'm using below query to list empty OU's fom AD. but its not working 🙁

    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

  • #8663

    Matt Tilford

    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.

  • #8664


    Here's an archived thread about the same topic from a while back:

    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.

You must be logged in to reply to this topic.