Get Last Logon Report in Office 365 using PowerShell

In this article you will learn how to get last login report in Office 365 for all users using PowerShell script.

Get last logon report using PowerShell

In this scenario we will use PowerShell to get last login report for all users in Office 365 environment. To achieve this we will use Get-MailboxStatistics PowerShell command along with LastLogonTime parameter.

Connect to Exchange Online

To be able to run below PowerShell script, you need to connect to Exchange Online module first. Open Windows PowerShell as administrator and run below PowerShell commands:

JavaScript
Install-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline

Get last login report in Office 365

To get last login report in Office 365 for all the users, copy the below PowerShell script and run it in Windows PowerShell where you are connected to Exchange Online module:

JavaScript
# Get all users
$AllUsers = Get-Mailbox -ResultSize Unlimited

# Loop through each user
foreach ($User in $AllUsers) {
    $UserPrincipalName = $User.UserPrincipalName
    
    # Get last logon time
    $LastLogon = Get-MailboxStatistics -Identity $UserPrincipalName | Select-Object -ExpandProperty LastLogonTime

    # Display user details on screen
    Write-Host "User Principal Name: $UserPrincipalName"
    Write-Host "Last Logon Time   : $LastLogon"
    Write-Host "-----------------------------"
}

The above script will display the Email Address of the users along with the Last Logon Time of all the users in your Exchange Online organization as shown below:

last logon report office 365 powershell

Export Last Logon report in CSV

To export the last logon report in CSV file, run below PowerShell script. Make sure to replace the path where you want to save the CSV file.

JavaScript
# Get all users
$AllUsers = Get-Mailbox -ResultSize Unlimited

# Create an array to store results
$LastLogonReport = @()

# Loop through each user
foreach ($User in $AllUsers) {
    $UserPrincipalName = $User.UserPrincipalName
    
    # Get last logon time
    $LastLogon = Get-MailboxStatistics -Identity $UserPrincipalName | Select-Object -ExpandProperty LastLogonTime

    # Create a custom object with user details
    $UserDetails = [PSCustomObject]@{
        UserPrincipalName = $UserPrincipalName
        LastLogon = $LastLogon
    }
    
    # Add user details to the report array
    $LastLogonReport += $UserDetails
}

# Output the report
$LastLogonReport | Export-Csv -Path "LastLogonReport.csv" -NoTypeInformation

The above script will fetch last logon report for all the user, and will export the results in a CSV file as shown below:

Export Last Logon report in CSV

Conclusion

In this article you learnt how to get last logon report for all the users in Office 365 using PowerShell.

You might like our other articles on Remove users access from Shared Mailbox in bulk, Get mailbox folder size using PowerShell, Get Exchange Online mailbox size in GB and Get Office 365 mailbox size report using PowerShell.

Please join us on YouTube for the latest videos on the Cloud Technology, and join our Newsletter for the early access of the articles and updates.

Happy Scripting!!