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