This is the first version of a script that uses .NET to query Active Directory. Some limitations apply at the moment, such as a proper filtering system, but that will hopefully be added in future updates.

# Get-ADObject.ps1
# Version 1.0
#Requires -Version 2

Get object from Active Directory

The name of the object you want to retrieve

The type of object you want to retrieve (examples: user,computer,group)

Active Directory domain

Get-ADObject Server001 -ObjectType Computer -Domain

Get computer object for Server001

Get-ADObject server* -Domain

Get all objects that starts with server

Get-ADObject * -ObjectType user

Get all user objects in the current domain

This script will get object data from Active Directory


	[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
	[String]$ObjectType = "*",
	[String]$Domain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name

	# Build LDAP string for AD root
	$domainSplit = $domain.Split(".")
	$adRootLDAP = "LDAP://"
	foreach ($split in $domainSplit){$adRootLDAP_tmp += ",DC=$($split)"}
	$adRootLDAP += $adRootLDAP_tmp.TrimStart(",")

	# Define AD Root
	$adRoot = New-Object System.DirectoryServices.DirectoryEntry($adRootLDAP)

		# Build LDAP Search Filter
		$adFilter = "(&(objectClass=$($ObjectType)) (Name=$ObjectName))"
		# Set up AD Searcher
		$adSearcher = New-Object System.DirectoryServices.DirectorySearcher($adRoot)
		$adSearcher.Filter = $adFilter
		# Perform search
		$adSearchResult = $adSearcher.FindAll()
		# Output object
		Write-Output $adSearchResult

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s