First you will need to connect to Office 365 Exchange, to do this:

$LiveCred = Get-Credential

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

Import-PSSession $Session

The simplest way of getting all users last log in times is by running the following command:

get-mailbox | get-mailboxstatistics | ft displayname, LastLogonTime

However if you have a lot of users in Office 365 then you may require to export this list to a csv file. The following command will do this for you:

(Get-Mailbox) | Foreach {Get-MailboxStatistics $_.Identity | Select DisplayName, LastLogonTime} | Export-CSV C:\LastLogonDate.csv

It’s always best to close the session once you have finished administrating with PowerShell. To do this:

Remove-PSSession $Session

Share