Get current user UPN

1 minute read

A few days ago while developing a cmdlet for an internal module in support to a larger automation workflow I found myself in need to easily derive UserPrincipalName of the currently logged on user running the command.

If you look this up in your favorite search engine chances are you came across results like the following (there are more but these are the more common suggestions from users):

# 1

# 2

# 3

# 4
$(Get-WMIObject -class Win32_ComputerSystem | select username).username

All the above methods are more or less equivalents and will yield the following results:

  1. PsCustomObject - Which is my samAccountName
  2. domain\PsCustomObject - Same as above just with the domain prefix
  3. PsCustomObject - Identical to number 1
  4. domain\PsCustomObject - Identical to number 2

Issue is none of them will easily return the UserPrincipalName of the currently logged in user.

This can easily be achieved with the following command:


This command, despite not as user friendly as the other commands, will return the full UPN of the user for example

I encourage you to explore the other available methods and properties of the [System.Security.Principal.WindowsIdentity] class as it can be really handy when trying to get details about the current user.