Last login time for 120 mailboxes

Microsoft Q & A 381 Reputation points
2023-06-14T16:24:44.0933333+00:00

Please help me to run a command to get the last login time of 120 mailboxes in office 365. I have all the mailboxes in a csv file.

Please help me to generate last login details in a csv.

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,182 questions
{count} votes

Accepted answer
  1. Konstantinos Passadis 19,591 Reputation points MVP
    2023-06-14T16:35:05.28+00:00

    Hello @Microsoft Q & A !

    Here is the Script :

    # Import the Exchange Online module
    Import-Module ExchangeOnlineManagement
    
    # Connect to Exchange Online
    Connect-ExchangeOnline -UserPrincipalName ******@mydomain.com
    
    # Read the mailbox list from CSV
    $mailboxes = Import-Csv -Path "C:\works\Mailboxes.csv"
    
    # Initialize an empty array to store the results
    $results = @()
    
    # Iterate through each mailbox
    foreach ($mailbox in $mailboxes) {
        $userPrincipalName = $mailbox.PrimarySmtpAddress
    
        # Get the last login time for the mailbox
        $lastLoginTime = Get-MailboxStatistics -Identity $userPrincipalName | Select-Object -ExpandProperty LastLogonTime
    
        # Create a custom object with the mailbox details
        $result = [PSCustomObject]@{
            UserPrincipalName = $userPrincipalName
            LastLoginTime = $lastLoginTime
        }
    
        # Add the result to the array
        $results += $result
    }
    
    # Export the results to CSV
    $results | Export-Csv -Path "C:\works\LastLoginDetails.csv" -NoTypeInformation
    
    
    

    Please change the Folder Paths and the User and you are good to go !

    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.