list empty OU's

Tagged: 

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Poshoholic Poshoholic 3 years, 9 months ago.

  • Author
    Posts
  • #8644
    Profile photo of nidhinck
    nidhinck
    Participant

    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
    Profile photo of Matt Tilford
    Matt Tilford
    Participant

    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
    Profile photo of Poshoholic
    Poshoholic
    Member

    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.

You must be logged in to reply to this topic.