4
votes

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, [email protected] 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 [email protected])

2

2 Answers

5
votes

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 /?
1
votes

I'm not sure how official this is, but I found a link in the registry that contains the username which is [email protected]. 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.