In organizations where too many accounts were setup, and ExAdmins’ primary job to keep the accounts neat and tidy, and this is a handy tool for these admins to find out the stale accounts and disable them and finally in no longer required; delete them from the system.
The command has a switch that will let you find out which users aren’t logging in at all at least for some days, if the user does not log into the Exchange mailbox within this amount of time then the mailbox gets disabled after the customary amount of given time expires. Now let’s take a look at the script followed by the description. Issue the following command:
Get-MailboxStatistics | where {$_.LastLogonTime -lt (get-date).addDays(-60)} | Disable-Mailbox
You can alter the service account logon by issuing the below command also:
Get-MailboxStatistics -server “YourServerName” | Where {($_.LastLogonTime
-gt (get-date).adddays(-60)) -and -not ($_.LastLoggedOnUserAccount
-like “*ServiceAccount*”)}
More info in the LastLogonTime Property is located HERE.
Allow me to explain the command a bit. First the command gets account info regarding the last logon time or the last time the account was accessed. So I’ve put the number 60, meaning for a particular user who has not logged in within 60 days or higher will be searched out. And finally the disable-mailbox switch will exactly do what it means, disabling those searched mailboxes, since they haven’t been accessed after a period of time. Afterwards you can delete these accounts from the purge setting from your exchange mailbox.
i.e: Use this command at your own risk J
This applies to Exchange 2007, Exchange 2010 and Exchange 2013