Get last logon times for all Exchange Online users

If you want to figure out when your users last logged on, perhaps to clean up licenses in use by dormant accounts, the following Powershell code may help you.


########
#checkLastLogonTimes
#Copyright: Free to use, please leave this header intact
#Author: Jos Lieben (OGD)
#Company: OGD (http://www.ogd.nl)
#Purpose: Generate a CSV file with last logon times of all Office 365 users
########

$csv = "c:\temp\LastLogons_$(Get-Date -format dd_MM_yyyy).csv"
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$users = get-mailbox -ResultSize Unlimited | select UserPrincipalName
Foreach ($user in $users){
$mbx = get-mailboxstatistics -Identity $($user.UserPrincipalName) | Select LastLogonTime
$upn = $user.UserPrincipalName
if ($mbx.LastLogonTime -eq $null){
$res = "Never"
}else{
$res = $mbx.LastLogonTime
}
$outStr = "$upn,$res"
Out-File -FilePath $csv -InputObject $outStr -Encoding UTF8 -append
}

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments