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()
$user.userprincipalname
}

References:
http://www.kli.dk/2008/09/17/166
https://jesperarnecke.wordpress.com/2013/05/14/get-a-domain-controller-from-any-domain-member/comment-page-1/#comment-23
https://technet.microsoft.com/en-us/library/ff730967.aspx

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s