Get Last Logon Time of Office 365 Users PowerShell

Meanwhile, I am performing cloud audit and wrote this “Get Last Logon Time of Office 365 Users PowerShell” for Cloud Administrators. Truly, auditing office 365 users to get last login time using PowerShell is important task. Hence, I am carrying out cloud audit with required to find inactive exchange online users. Firstly, I need to get last login date for all office 365 mailbox enabled users. Secondly, exchange online users who are inactive for last 90 days. Finally, I am saving the results to a csv file using PowerShell Export-Csv command.

Connect to Exchange Online

Above all, you need to connect Exchange Online using PowerShell by using following commands.

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline

Meanwhile, you can view Connect to Exchange Online using PowerShell tutorial and the purpose discussed in this post to connect exchange online.

Get Last Logon Time of Office 365 Users

Certainly, I am using Exchange Online PowerShell cmdlet Get-EXOMailbox and Get-EXOMailboxStatistics to Get Office 365 users Last Logon Time. I am using below PowerShell code snippet.

$mailboxes = Get-EXOMailbox -ResultSize Unlimited
$mailboxes | ForEach-Object {
 $mbx = $_
 $mbs = Get-EXOMailboxStatistics -Identity $mbx.UserPrincipalName | Where-Object LastLogonTime -LE (Get-Date).AddDays(-90) | Select DisplayName, LastLogonTime
  if ($mbs.LastLogonTime -ne $null){
        $lt = $mbs.LastLogonTime
  }else{
        $lt = "Normal User" }
 
    New-Object -TypeName PSObject -Property @{
        DisplayName = $mbx.DisplayName
        UserPrincipalName = $mbx.UserPrincipalName
        LastLogonTime = $lt}
}

List of Inactive Users in Microsoft 365

Above Exchange Online PowerShell code snippet will help you to find officer 365 users who are inactive from last 90 days. Of course, you can change number of days in this line to meet your requirement Where-Object LastLogonTime -LE (Get-Date).AddDays(-90).

Finally, I am able to list down all the inactive users to reduce my monthly licensing cost. During this PowerShell code snippet, I faced that Powershell output is cut by Powershell … three dots. Now, you can use Format-Table, Out-GridView or $FormatEnumerationLimit =-1 to create a wider display.

Export Office 365 Users Last Logon Time to CSV

Accordingly, you can export office 365 users last login time to CSV file as well. In fact, you need to add Format-Table -Property * -AutoSize | Out-File -FilePath D:\PowerShell\MailboxAuditReport.csv -NoClobber to save the file. I am writing the complete code again for your reference.

$mailboxes = Get-EXOMailbox -ResultSize Unlimited
$mailboxes | ForEach-Object {
 $mbx = $_
 $mbs = Get-EXOMailboxStatistics -Identity $mbx.UserPrincipalName | Where-Object LastLogonTime -LE (Get-Date).AddDays(-90) | Select DisplayName, LastLogonTime
  if ($mbs.LastLogonTime -ne $null){
        $lt = $mbs.LastLogonTime
  }else{
        $lt = "Normal User" }
 
    New-Object -TypeName PSObject -Property @{
        DisplayName = $mbx.DisplayName
        UserPrincipalName = $mbx.UserPrincipalName
        LastLogonTime = $lt}
} | Format-Table -Property * -AutoSize | Out-File -FilePath D:\PowerShell\MailboxAuditReport.csv -NoClobber

Get Last Logon Time of AzureAD Users

Further, I posted this post on Microsoft community page and found few comments. Simultaneously, comments was meant that AzureAD users and Exchange online users have different in numbers, licensed or unlicensed users. Meanwhile, I compared also and found the exact same 613 users in AzureAD and Microsoft 365 / Office 365 environment. Now, for cloud admins who need same script for AzureAD Users, I wrote this article.

References

Share

You may also like...