Find Last Logon Time for Office 365 Users With Powershell

MicrosoftExchange OnlinePowershell

Find Last Logon Time for Office 365 Users is a critical component of effective administration and management within the platform. This process serves multifaceted purposes, primarily revolving around security, resource optimization, compliance adherence, user support, and troubleshooting. From a security standpoint, identifying the last logon time enables the prompt detection of any suspicious or unauthorized activities, providing administrators with a proactive approach to address potential security threats.

On the resource management front, this information facilitates the efficient identification and handling of inactive user accounts, ensuring streamlined resource allocation and optimal licensing. Compliance requirements are met through the maintenance of accurate user logon records, contributing to comprehensive compliance reporting and adherence to regulatory standards.

Find Last Logon Time for Office 365 Users

In the Office 365 administration, gaining insights into user activities is pivotal. The ability to Find Last Logon Time for Office 365 Users provides administrators with a powerful tool for efficient user management.Moreover, understanding the last logon time proves invaluable for user support and troubleshooting efforts. Administrators can utilize this data to provide targeted assistance, especially when users report issues, enhancing the overall user experience.

Need for the Script

Determining the last logon time for Office 365 users is essential for various reasons, including:

  • Security Monitoring: Detect suspicious or unauthorized logon activities.
  • Resource Management: Efficiently identify and manage inactive user accounts.
  • Compliance: Fulfill auditing and compliance requirements by keeping track of user logon details.

Connecting with Office 365

Before delving into the script, establishing a connection with Office 365 is imperative. This ensures that subsequent operations are performed within the Office 365 environment.

Module Installation

To interact with Office 365 services using PowerShell, the appropriate module needs to be installed. Run the following command to facilitate the installation:

Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber

Import Module and Connect

Once the module is installed, import it into the PowerShell session and establish a connection with Office 365:

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName <YourUPN> -ShowProgress $true

Replace <YourUPN> with your Office 365 user principal name.

Script Writing

Now, let’s explore the PowerShell script designed to find the last logon time for Office 365 users.

$Result=@() 
$mailboxes = Get-Mailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1 
$mailboxes | ForEach-Object {
    $i++
    $mbx = $_
    $mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName | Select LastLogonTime
    if ($mbs.LastLogonTime -eq $null){
        $lt = "Never Logged In"
    } else {
        $lt = $mbs.LastLogonTime
    }
    Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
    $Result += New-Object PSObject -property @{ 
        Name = $mbx.DisplayName
        UserPrincipalName = $mbx.UserPrincipalName
        LastLogonTime = $lt 
    }
}
$Result | Export-CSV "C:\O365-LastLogon-Info.csv" -NoTypeInformation -Encoding UTF8

Modifications

  • File Path: Ensure to change the export file path. The current path is “C:\O365-LastLogon-Info.csv.”

Script Breakdown

  1. Initialization:
    • $Result=@(): Initializes an array to store the results.
    • $mailboxes = Get-Mailbox -ResultSize Unlimited: Retrieves all mailboxes in Office 365.
    • $totalmbx = $mailboxes.Count: Determines the total number of mailboxes.
    • $i = 1: Initializes a counter for tracking progress.
  2. ForEach Loop:
    • Iterates through each mailbox in $mailboxes.
  3. Logon Time Retrieval:
    • Retrieves the last logon time for each mailbox using Get-MailboxStatistics.
    • Handles cases where the last logon time is null, indicating never logged in.
  4. Progress Tracking:
    • Utilizes Write-Progress to display progress information.
  5. Result Compilation:
    • Compiles the results into a custom PowerShell object.
  6. Export to CSV:
    • Exports the results to a CSV file specified by the file path.

Conclusion

In conclusion, the provided PowerShell script offers a robust solution to Find Last Logon Time for Office 365 Users. By incorporating this script into your administration tasks, you can enhance security, streamline resource management, and meet compliance requirements effectively.

I hope you find this article useful.

Leave a Reply

Your email address will not be published. Required fields are marked *