
close
close
Over the course of the last several articles, I have been demonstrating how to find Active Directory objects using the Active Directory Searcher Object from .NET and PowerShell. I have purposely kept my examples simple and constrained but today we are running free. I need to show you how to manage large environments.
I know that my domain has 6351 user accounts. Let’s see what I get with the searcher object.
$searcher = New-Object system.DirectoryServices.DirectorySearcher $searcher.filter = "(objectclass=user)" $all = $searcher.FindAll()
advertisment
$searcher.PageSize=100 $all = $searcher.FindAll() $all.count
I have experimented with different page sizes and cannot seem to find any value that is necessarily better than another. I tend to use 100 but you should experiment in your own test environment.
Regardless, $all now has 6447 objects! I have cleared the first hurdle and appear to be getting more than 1000 objects. However, I have even more than expected.
My searcher is defaulting to the domain root, so I know it is not missing anything. Using my Convert-ADSearchResult function from a previous article, I can group the results on the object type.
$searcher.filter = "(&(objectcategory=person)(objectclass=user))" $all = $searcher.FindAll()
If you recall in a previous article, I showed you how to create a compound filter. Let’s check again.
$searcher.filter = "(&(objectcategory=person)(objectclass=user)(name=mysampleuser3*))" $searcher.findall()
$searcher.filter = "(&(objectcategory=person)(objectclass=user)(name=a*)(title=tester))" $searcher.findall()
advertisment
$searcher = New-Object system.DirectoryServices.DirectorySearcher $searcher.filter = "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))" $searcher.PageSize = 100
There is no way I could have figured out that query on my own.
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group