4

I'm trying to get the current Windows username & domain from Powershell on a Windows 10 Azure Active Directory (AAD) joined machine.

I've tried the tips at this question, but none of them seem to work for Azure Active Directory-joined machines.

e.g. for the user: Jonathan Doe, john@example.com you'll get only the users' proper name & AzureAD (not their username or 'real' domain):

$env:UserName --> JonathanDoe
$env:UserDomain --> AzureAD
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name --> AzureAD\JonathanDoe

Does anyone know how to get any part of the user's actual credential or specific Azure AAD domain? (e.g. john or example.com or ideally john@example.com)

Community
  • 1
  • 1
semenko
  • 73
  • 1
  • 1
  • 6

2 Answers2

5

You can run the following command in PowerShell, the output will display the user name in UPN format.You can get both of the username and domain name from that.

whoami.exe /UPN

In addition, the program 'whoami.exe' provides many other parameters for getting additional information about current user. You can type the following command for more details about 'whoami.exe'.

whoami.exe /?
Andy Liu - MSFT
  • 575
  • 3
  • 7
  • 3
    Most useful but running cmd line tools and capturing the output is a common process, it always smacks of a fudge. Anyone know of a .NET or WinAPI call that achieves the same as whoami.exe /UPN? – Rob Nicholson Oct 24 '18 at 14:00
2

I'm not sure how official this is, but I found a link in the registry that contains the username which is user@company.com. This was under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IdentityStore\Cache\xxx\IdentityCache\xxx. The key name was UserName. You can use the built-in powershell registry provider to navigate to this registry entry.

CtrlDot
  • 2,463
  • 14
  • 11
  • There appears to be multiple keys with very similar information in that area of the registry. However, I guess you could match $Env:Username against SAMName in the first one you found. Bit smelly code :-) – Rob Nicholson Oct 24 '18 at 14:02