none
Grabbing all OU's while ignoring their all sub-OU's RRS feed

  • Question

  • I am working on a project where we grab all useful user data from the AD/all OUs. We are doing this in order to move a user if they are in the wrong OU. For this we want to ignore all sub-OU in every OU, we also want to do this with out hard coding in a bunch of -nolike statements as we have many sub-OU. From my time researching this it looks like the only way to do this is through hard coding. Is there a way of, when searching through AD, ignoring ALL sub-ou and just searching all OU. 

    Thanks for any help,

    Nicholas 

    Wednesday, July 17, 2019 1:16 PM

All replies

  • Try below:

    Get-ADUser -SearchBase "OU Path" -SearchScope 1 -Filter * 

    Wednesday, July 17, 2019 3:01 PM
  • Assuming you just want OU's where the parent is the domain object, you want the base of the query for OUs to be the DN of the domain, and the scope of the query to be oneLevel (the default is subtree, which would include child OUs). For example, using the AD module cmdlet:

    Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'DC=FABRIKAM,DC=COM' -SearchScope OneLevel

    Or using Get-ADObject:

    Get-ADObject -LDAPFilter '(objectClass=organizationalUnit)' -SearchBase 'DC=FABRIKAM,DC=COM' -SearchScope OneLevel
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Wednesday, July 17, 2019 3:08 PM
  • Thanks i will make sure to try this it sounds like it could work for what i need to do. 
    Wednesday, July 17, 2019 4:48 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Thursday, July 18, 2019 5:49 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, July 31, 2019 7:33 AM
    Moderator