Query Active Directory for OU's VB.Net

I'm trying to find some code that will help me query active directory and get a list of organizational units. I have some other code i was using to get the computer objects.. Now I'm trying to recode this to get OU's.  Can someone please help?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Clears listbox

        'Connect to AD Database
        Dim rootEntry As New DirectoryEntry("GC://DC=TonyNet,DC=org", Domain.Text + userID.Text, password.Text)

        'Set What you want to search for
        Dim searcher As New DirectorySearcher(rootEntry)

        'Set Your Search Filter
        searcher.Filter = "(objectCategory=Computer)"

        'Finds all
        Dim queryResults As SearchResultCollection
        queryResults = searcher.FindAll()

        'Puts the results into a list Box
        Dim result As SearchResult
        For Each result In queryResults

End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It may be really simple.
Change you filter from
       'Set Your Search Filter
        searcher.Filter = "(objectCategory=Computer)"

       'Set Your Search Filter
        searcher.Filter = "(objectCategory=organizationalUnit)"

Try that, and let me know
AnthonySmithMCPAuthor Commented:
Thank you for your help. When i run it.. I get a message saying "Index was out of range. Must be non-negative and less than the size of the collection. Perimeter name index"
AnthonySmithMCPAuthor Commented:
It's pointing to this line

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Your properties to load will need to change

These are VERY unlikely to be applicable to an OU :)
And because they aren't, you have results, but no applicable properties to punch into the listbox.
Hence the error

What OU properties do you want listed?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AnthonySmithMCPAuthor Commented:
Yep.. you're right, I needed to change the property to "name".  Is it anyway to change the search to NOT go to sub OU's?
AnthonySmithMCPAuthor Commented:
nvm got it.. lol just needed to use SearchScope.OneLevel
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.