Get User’s Full Name From Username

April 29, 2010 — 4 Comments

Here’s an extremely simple application that will query Active Directory for a user’s full name when given their username.

Compiled program download link:…/GetRealNameFromAD/

and here is the source code for the main function if anyone is interested:

''' <summary>
''' Finds a user's combined first name and surname from Active Directory given their username
''' </summary>
''' <param name="UsernameToFind">The username (sAMAccountName) to search for</param>
Private Function GetRealNameFromAd(ByVal UsernameToFind As String) As String
    Using searcher As New DirectorySearcher(New DirectoryEntry())
        searcher.PageSize = 1000
        searcher.SearchScope = SearchScope.Subtree
        searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
        Using Results As SearchResultCollection = searcher.FindAll
            If Results Is Nothing OrElse Results.Count <> 1 Then
                Throw New ApplicationException("Invalid number of results returned – either no users were found or more than one user account was found")
            End If
            Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
                Return CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
            End Using
        End Using
    End Using
End Function

4 responses to Get User’s Full Name From Username


    Hey Chris!
    I’m using this code in a project I’m working on at the moment, but had a question for you as you seem to be pretty competent with the Active Directory stuff.

    How can I get the “alias” of the currently logged in user from Active Directory/LDAP?

    Like at work, my login is h318129. Using the above code you posted, it retrieves my full name. How can I retrieve my email or alias (which is the first part of the email) from that login? The alias in my case is uss30. Email is



    As far as I know there is no such thing as an ‘alias’ in AD so the only thing I can help you with is getting the email address. All you need to do to get the email address is just replace this line in the above code:

    Return CStr(UserDE.Properties(“givenName”).Value) & ” ” & CStr(UserDE.Properties(“sn”).Value)

    with this:

    Return CStr(UserDE.Properties(“mail”).Value)

    Does that give you what you wanted?



    Very, very nicely done Chris. I’m working on some code to return the locked/not locked status of a specified user account. Please post a follow-up if can provide some direction on that.


      This is how I get the locked out status in my AD reporting tool, sorry but you’ll have to figure out what the variables and functions actually are as I haven’t got time to make it all generic and provide the whole thing as a stand alone function for you:

      If AdObject.Properties.Contains(“lockoutTime”) Then
      If CLng(AdObject.Properties(“lockoutTime”)(0)) = 0 Then
      Return False
      End If
      If _LockoutDuration = Nothing Then
      Dim RawValue = DirectCast(_DomainToSearchDirectoryEntry.Properties(“lockoutDuration”).Value, ActiveDirectoryHelper.IADsLargeInteger)
      Dim Ticks As Int64 = ActiveDirectoryHelper.GetTicksFromIADSLargeInteger(RawValue)
      Dim TempLockoutDuration As TimeSpan = TimeSpan.FromTicks(Ticks)
      If TempLockoutDuration = TimeSpan.MinValue Then
      _LockoutDuration = TempLockoutDuration
      _LockoutDuration = TimeSpan.FromTicks(-Ticks)
      End If
      Catch ex As Exception
      Throw New ApplicationException(“Unable to get lockout duration from domain security policy due to the following error: ” & ex.Message)
      End Try
      End If
      If _LockoutDuration = TimeSpan.MinValue Then
      Return True
      Dim TimeLocked As Date = ActiveDirectoryHelper.GetDateFromADInterval(AdObject, “lockoutTime”)
      Return TimeLocked.Add(_LockoutDuration) > Now
      End If
      Return False
      End If

Leave a Reply to cjwdev Cancel 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