Retired employees are usually reported to the IT department, but for whatever reason this is not always the case. As an Office 365 administrator, you might want to know which user haven’t logged in for a long time to tidy up your Office 365 environment and remove the orphaned user mailboxes. That’s what this blog post is about. Have fun reading.
Login to Exchange Online
You’ll net three PowerShell commands to log into Exchange Online, as described here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps
$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session -DisableNameChecking
For your convenience, I’ve created a function that enables you to login with only one command. Interested? Have a look: Office 365 Exchange Online PowerShell: Quick connect with Connect-ExchangeOnline
To make sure you are actually logged in Exchange Online, run an exchange specific command like Get-Mailbox.
Get-Mailbox
We are ready. Let’s move on.
Determine inactive Mailboxes
So, let’s examine all users with mailboxes that haven’t logged on in the last 10 days with the following PowerShell code:
Get-Mailbox –RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {$_.LastLogonTime –lt (Get-Date).AddDays(-10) } | Select-Object DisplayName, LastLogonTime
Nice one.
Normally, you will search for users who have not logged in for a long time. For that simply replace -10 with a value of your choosing.
Categories: Microsoft 365, PowerShell
Hello,
Get-MailboxStatistics can’t be used, it is not accurate
https://www.petri.com/get-mailboxstatistics-cmdlet-wrong
LikeLike