Search an object forest wide
Search an object forest wide

This script will search an object forest wide. The search is based on a LDAP filter. In my script, the result show the mail user attributes like “toto”.

Script :

#Get Domain List
$objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$DomainList = @($objForest.Domains | Select-Object Name)
$Domains = $DomainList | foreach {$_.Name}

#Act on each domain
foreach($Domain in ($Domains)) {
	Write-Host "Checking $Domain" -fore red
	$ADsPath = [ADSI]"LDAP://$Domain"
	$objSearcher = New-Object System.DirectoryServices.DirectorySearcher($ADsPath)
	#The filter
	$objSearcher.Filter = "(&(objectCategory=user)(mail=*toto*))"
	$objSearcher.SearchScope = "Subtree"
	
	$colResults = $objSearcher.FindAll()
	
	foreach ($objResult in $colResults) {
		$objArray = $objResult.GetDirectoryEntry()
		write-host $objArray.DistinguishedName ";" $objArray.mail ";" $objArray.ProxyAddresses "`r"
	}
}

References

DirectorySearcher Class

Searching Active Directory with Windows PowerShell

“At heart, Active Directory is nothing more than a database (a Jet database, to be exact). Big deal, you say? Well, as a matter of fact, it is a big deal: the fact that Active Directory is a database means that you can use scripts to search Active Directory. Need a list of all your user accounts? Write an Active Directory search script. Need a list of all your computer accounts? Write an Active Directory search script. Need a list of all your color printers or all your contacts from the Fabrikam Corporation? Write an Active Directory– well, you know how it goes by now.
Of course, it’s one thing to suggest that someone write an Active Directory search script; it’s quite another thing to actually sit down and write that Active Directory search script. That’s not because these scripts are hard to write; it’s because it’s very difficult to find documentation and examples that show you how to write Active Directory search scripts using Windows PowerShell.
Well, check that: it used to be very difficult to find documentation and examples that show you how to write Active Directory search scripts using Windows PowerShell.
The purpose of this article is straightforward: combined with 100+ sample scripts recently added to the Script Center Script Repository, this article provides an introduction to the fine art of writing Active Directory search scripts using Windows PowerShell. Does this article contain everything you’ll ever need to know about writing Active Directory search scripts? Probably not. But it does include enough information to help you get started.
Note. So where can you find more information on writing Active Directory search scripts? Good question. The Scripting Guys did a webcast on searching Active Directory a few years back, and they also put together a two-part Tales From the Script series entitled Dude, Where’s My Printer? Both the webcast and the columns use VBScript in their examples (after all, there was no such thing as PowerShell back in those days), but you still might find some of the “generic” information about Activity Directory (What’s a scope? What’s an attribute? What’s a page size?) to be useful.

By the way, these days all the excitement in the PowerShell world revolves around PowerShell 2.0 and the November 2007 Community Technology Preview release. Because of that, we thought it was important to stress that the ability to writes scripts that search Active Directory does not require PowerShell 2.0. All the sample code you’ll see today works equally well on both versions of PowerShell. If you’ve got either version of Windows PowerShell (1.0 or 2.0) installed then you’re ready to write Active Directory search scripts.”

<>
Search an object forest wide

Leave a Reply

Your email address will not be published.