AD User information from member server using Powershell

So I needed to get the userprincipal name from the domain, just to make sure that it was matching what I was expecting. No Powershell modules, no tools to aid besides Read access on the domain from the Domain Users membership. So how to pull that off?
A lot of long lined scripts, except for one. I took that and edited a few things from it and was able to get exactly what I needed, within a few simple lines. Also reusing the comment from Mattias R. Jessen in one of my previous posts.

$Username = 'jesper.arnecke'

#This search will target the domain from Root level. For refines on specific OU's, use the references.
$Search = New-Object DirectoryServices.DirectorySearcher
#Page size in order to return more than 1000 objects per query.
$Search.PageSize = 1000
#Filter definition
$Search.filter = "(&(objectCategory=person)(SAMAccountname=$Username))"
#Defines the search scope depth. Available choices are "Base", "OneLevel" and "Subtree"(Default value), 
$objSearcher.SearchScope = "Subtree"

Foreach($result in $Search.Findall()){
$user = $result.GetDirectoryEntry()


16-September-2015: Added more comments, properties and comments. Added additional reference.

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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.