Connect to Exchange Online PowerShell
Exchange Online PowerShell Modunle
You need install Exchange Online module only once. In the next time, run to connect to Exchange Online PowerShell.
##Connect to Exchange Online.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
Install-Module -Name ExchangeOnlineManagement;
Import-Module ExchangeOnlineManagement;
Connect-ExchangeOnline;
Last logon time for single mailbox
$email = read-Host 'Email address to get last logon time'
$upn=(Get-ExoMailbox -Identity $email).UserPrincipalName
$CreationTime=(Get-MailboxStatistics -Identity $email).WhenCreated
$LastLogonTime=(Get-MailboxStatistics -Identity $email).lastlogontime
$DisplayName=(Get-MailboxStatistics -Identity $email).DisplayName
$InactiveDaysOfUser= (New-TimeSpan -Start $LastLogonTime).Days
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastLogonTime'=$LastLogonTime;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser}
$Output= New-Object PSObject -Property $Result
$Output | Select-Object UserPrincipalName,DisplayName,LastLogonTime,CreationTime,InactiveDays,MailboxType | FT
##End.
Last logon time for all mailboxes
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
mkdir $env:temp\o365; cd $env:temp\o365;
Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Search Mailbox"} | ForEach-Object{
$upn=$_.UserPrincipalName
$CreationTime=$_.WhenCreated
$LastLogonTime=(Get-MailboxStatistics -Identity $upn).lastlogontime
$DisplayName=$_.DisplayName
$MBType=$_.RecipientTypeDetails
$Print=1
$MBUserCount++
Write-Progress -Activity "`n Processed mailbox count: $MBUserCount "`n" Currently Processing: $DisplayName"
#Retrieve lastlogon time and then calculate Inactive days
if($LastLogonTime -eq $null)
{
$LastLogonTime ="Never Logged In"
$InactiveDaysOfUser="-"
}
else
{
$InactiveDaysOfUser= (New-TimeSpan -Start $LastLogonTime).Days
}
#Export result to CSV file
if($Print -eq 1)
{
$OutputCount++
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastLogonTime'=$LastLogonTime;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser;'MailboxType'=$MBType}
$Output= New-Object PSObject -Property $Result
$Output | Select-Object UserPrincipalName,DisplayName,LastLogonTime,CreationTime,InactiveDays,MailboxType,AssignedLicenses,Roles |
Export-Csv $env:temp\o365\LastLogonTimeReport_$CurrentDate.csv -Notype -Append
}
}
ii $env:temp\o365;
##End.
5/5 - (1 vote)