Retrieve current domain user's full name

Solution 1:

$dom = $env:userdomain
$usr = $env:username
([adsi]"WinNT://$dom/$usr,user").fullname

Returns:

John Doe

Some other (mostly) obscure properties also available. A few useful ones:

  • Homedrive UNC
  • Homedrive Letter
  • Description
  • Login script

Try:

[adsi]"WinNT://$dom/$usr,user" | select *

Solution 2:

I like the accepted answer, but just because I wanted to try this out myself:

$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName

returns:

FullName
--------
TheCleaner

or if you wish to not have the header info and just the result:

$user = whoami
Get-WMIObject Win32_UserAccount | where caption -eq $user | select FullName | ft -hide

Solution 3:

One liner using Powershell 3.0:

gwmi win32_useraccount | where {$_.caption -match $env:USERNAME} | select fullname | ft -HideTableHeaders

Solution 4:

Based on your comment on Craig620's accepted answer,

Do I need domain admin rights to run this command? Or can the domain user itself can run this command?

It sounds like you're trying to avoid installing powershell modules on user workstations, yes, but also, no, you don't need to be a domain admin to look up your own name in AD. You can look up pretty much any information that appears in the GAL in Outlook, including full name, as a standard user.

You can also look up other people's full names as a standard user in AD (using Get-WmiObject Win32_userAccount, if you want to avoid the AD modules). Service accounts that query AD (well, prior to managed service accounts) are usually standard, unprivileged AD users.


Solution 5:

If you've always got .Net 3.5 or higher (which you should with PowerShell v4.0 and higher):

Add-Type -AssemblyName System.DirectoryServices.AccountManagement;
$DisplayName = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current.DisplayName;

That class provides very easy access to all the common LDAP properties, so you don't need to lookup twice (once with WinNT and again with LDAP) or use [ADSISearcher] to do an LDAP search if you want some extended properties that WinNT doesn't implement.