Usernames from Email exchange 365

If you have email addresses and need to get usernames ( this is for the cloud based exchange 365 ) :-

Open powershell and :

Import-Module ActiveDirectory

Also logon to Exchange environment Exchange 2010 in the cloud:-

#Logon

$LiveCred = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Import-PSSession $Session

 

Then the script is:-

Get-Content emailtest.csv | %{

Get-Mailbox $_ | Select-Object PrimarySmtpAddress, UserPrincipalName

} | Export-Csv users.csv -NoTypeInformation