Fast Way to Get User Attributes from AD Without using Get-ADUser

Get-ADUser is slow and cumbersome to get information from, and required the AD module to be installed on the computer. Here is a faster way to get information from AD, without that PowerShell Module.

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher.Filter = "(&(objectCategory=User)(SamAccountname=$($loggedinUser)))"
$objSearcher.SearchScope = "Subtree"
$obj = $objSearcher.FindOne()
#Use the attributes found in AD to get properties, with example used below
$FullName =$obj.Properties["cn"]
$JobTitle =$obj.Properties["description"]
$StreetAddress =$obj.Properties["streetAddress"]
$City =$obj.Properties["l"]
$State =$obj.Properties["st"]
$ZipCode =$obj.Properties["postalCode"]
$PhoneNumber =$obj.Properties["homePhone"]
$FaxNumber =$obj.Properties["facsimileTelephoneNumber"]

Comments

Popular posts from this blog

SCCM PXE Boot Issues - No Advertisements Found

SCCM PXE Boot Issues - UEFI and Secure Boot

Application Doesn't Install During OSD Task Sequence in SCCM