list empty OU's

Welcome Forums General PowerShell Q&A list empty OU's

This topic contains 2 replies, has 3 voices, and was last updated by

5 years, 4 months ago.

  • Author
  • #8644

    Points: 0
    Rank: Member

    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

    Points: 2
    Rank: Member

    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

    Points: 1
    Rank: Member

    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.

The topic ‘list empty OU's’ is closed to new replies.