If you’ve ever struggled to find out who your current session is logged in as when you connected to Office 365, here’s a tidbit to shed some light on it:
(Get-PSSession |?{$_.ComputerName -like “*outlook.com”})[0].RunSpace.ConnectionInfo.Credential.UserName

You can also use RunSpace.OriginalConnectionInfo.Credential.UserName. The SDK says ConnectionInfo vs OriginalConnectionInfo:
ConnectionInfo – Connection information for remote Runspaces, null for local Runspaces
OriginalConnectionInfo – ConnectionInfo originally supplied by the user
I have found them to have the same output in the context of Office 365 to this point, but if any of my readers stumbles upon a difference, I’d be happy to learn!


This really isn’t a WhoAmI for Office 365 but for Exchange Online. When using the Exchange Online Management V2 PowerShell module, the command needs to change to this:
(Get-PSSession |?{$_.ComputerName -like “outlook.office365.com”})[0].RunSpace.ConnectionInfo.Credential.UserName
There really needs to be a WhoAmI for the O365, AzureAD, and Exchange Online modules. Honestly for all M365 modules.
If there is a WhoAmI for the MSOL module then you can use that information to automatically create the login command for the Exchange Online and seamlessly login:
Connect-ExchangeOnline -DelegatedOrganization -UserPrincipalName