28

Using PowerShell, how can I get the currently logged on domain user's full name (not only its username) without the need of the ActiveDirectory module?

Kazark
  • 117
  • 5
Jonathan Rioux
  • 1,878
  • 6
  • 33
  • 57

9 Answers9

31
$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 *
Mark Wragg
  • 226
  • 2
  • 12
Clayton
  • 4,483
  • 16
  • 24
16

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
TheCleaner
  • 32,352
  • 26
  • 126
  • 188
7

One liner using Powershell 3.0:

gwmi win32_useraccount | where {$_.caption -match $env:USERNAME} | select fullname | ft -HideTableHeaders
MDMoore313
  • 5,531
  • 6
  • 34
  • 73
3

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.

Katherine Villyard
  • 18,510
  • 4
  • 36
  • 59
2
([adsi]"LDAP://$(whoami /fqdn)").displayName

You can retrieve a truckload of information using this very simple tool. Check out

([adsi]"LDAP://$(whoami /fqdn)") | fl *
KeyszerS
  • 180
  • 9
2

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.

Bacon Bits
  • 1,511
  • 1
  • 9
  • 8
1

Using -match is not a good choice because a $env:USERNAME of "ed" will match "fred" and "edith". Instead use -eq for an exact match and add in the domain if needed. I use a foreach loop at the end to strip off all leading an trailing whitespace as an alternative to "select fullname | ft -HideTableHeaders" which prints a leading and trailing newline.

gwmi win32_useraccount | where {$_.caption -eq $("domain\" + $env:USERNAME)} | foreach {$_.fullname}
0

How about querying the registry instead of AD Like this:

if ((gwmi win32_computersystem).partofdomain -eq $true)
{Get-ItemPropertyValue -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\1 -Name "LoggedOnDisplayName"}
else 
{Get-ItemPropertyValue -Path HKLM:\SOFTWARE\Microsoft\Windows\Cu rrentVersion\Authentication\LogonUI\SessionData\1 -Name "LoggedOnUser" | %{$_.Split('\')[1]}}

Note: Only tested on Windows 10.

Another Note: this looks for the first logged user in the current session, so for example if you logged out of john.smith and logged in will.smith and run the above you will get the data related to john.smith instead of will.smith.

Update: The below script will get the current user's display name regardless of being on a domain joined PC or not or logging in with another account before logging in with the account that you need to get the display name for.

$user = (Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData\" | Get-ItemProperty | Where-Object LoggedOnUser -like "$(whoami)" | select -First 1)

if ((Get-Item $user.PSPath).Property -contains "LoggedOnDisplayName" -eq $true) {
    $user.LoggedOnDisplayName
} else {
    $user.LoggedOnUser | %{$_.Split('\')[1]}
}
Mostafa
  • 1
  • 1
0

If you don't want to use the Active Directory module, you can't; unless you want to go even deeper and perform an actual LDAP query against a domain controller.

Any user information other than the username is stored in Active Directory, and it has to be retrieved there.

Massimo
  • 68,714
  • 56
  • 196
  • 319