List active Exchange mailboxes using PowerShell

List of active mailboxes in Powershell

Let’s start with the most basic activity report – a list of users’ and shared mailboxes sorted starting from the most recent logon time.

Here is the script that will let us generate it:

Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox | Get-MailboxStatistics | Sort-Object lastlogontime -Descending | Select-Object DisplayName,LastLogonTime

If you don’t need the whole list, just a certain number of most active mailboxes, use the -First parameter in the Select-Object cmdlet:

Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox | Get-MailboxStatistics | Sort-Object lastlogontime -Descending | Select-Object –First <number> -Property DisplayName,LastLogonTime

Keep in mind that the script above won’t let you control the activity time range in your list. No worries – that’s where the next script comes in:

Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox | Where {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)} | Sort -Property @{e={( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending | Select-Object DisplayName,@{n="LastLogonTime";e={(Get-MailboxStatistics $_.Identity).LastLogonTime}}

The key part is the configuration of the Where filter.

In my example it’s configured to list mailboxes which were active in the last 30 days:

(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)

But it can be just as easily set up to check activity by hours, minutes, seconds or years. Learn more about date arithmetic in the Get-Date cmdlet

Tips

All of the above results can be exported to CSV files by piping the cmdlets to Export-Csv.

To list inactive mailboxes, simply leave out the -Descending switch in each script.

Further reading

Tools for Microsoft 365

Recommended articles

How to export users from Active Directory

How to export users from Active Directory

You might need to export users from Active Directory in more than one situation. Good examples include Exchange migration and creating a test Exchange environment. You can imagine how painful it would be to do those tasks manually, especially in a large organization. Luckily, users can be exported easily from Active Directory and saved into a CSV (comma separated value) file. In this article I am going to show you how to do it. Watch the video below for a quick walkthrough and read the following article if you want to learn more.
How to migrate Exchange public folders to a shared mailbox in Office 365

How to migrate Exchange public folders to a shared mailbox in Office 365

This article shows, step by step, how to easily migrate Exchange public folders to an Office 365 (Microsoft 365) shared mailbox using CodeTwo Migration software. The article also contains a guide on how to create a shared mailbox in Office 365 and how to access it from a mobile device.
New-ComplianceSearch: how to use the newer version of Search-Mailbox

New-ComplianceSearch: how to use the newer version of Search-Mailbox

Microsoft retired the Search-Mailbox cmdlet – now what? Discover how to use New-ComplianceSearch, its key advantages and how to make the switch seamlessly.

Comments

  1. Exchange 2013 from MS:

    As we discussed, I understand you’re talking about the ‘LastLogonTime’ property for the mailbox statistics. In older versions of Exchange this property was indeed used to track the last login time of a mailbox, but now has become a bit of a misnomer. While you previously it might have indicated the last time a Delegate or Owner logged into the mailbox, currently it is used to indicate the last time the mailbox was accessed, in most cases by a mailbox assistant, so it’s not a good metric for tracking the actual usage of a mailbox.
    If you needed to know the exact timestamp of the last logon and it is within the logging timeframe (max 90 days), you can pull a audit log for each mailbox and look for the MailboxLogin event.

    https://docs.microsoft.com/en-us/Exchange/policy-and-compliance/mailbox-audit-logging/mailbox-audit-logging?view=exchserver-2019

    Unfortunately I was not able to find any official documentation that state that the ‘LastLogonTime’ is a supported parameter or not. I will keep looking from this documentation and in case I’m not able to find it will submit the request with our Team.

  2. I am getting an error running the Where>number of days script. Exchange Management Shell Pipeline Not Executed Because a Pipeline Is Already Executing..

    • avatar
      Adam the 32-bit Aardvark says:

      Please try saving get-mailbox results into a variable and then substitute the first part of the script with the variable:
      $Mailboxes = Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox;
      $Mailboxes | Where...

    • avatar
      Adam the 32-bit Aardvark says:

      Hi Brian,
      Where is an alias for the Where-Object cmdlet. Which means that, unless you change or remove this default alias, Where and Where-Object will give you the same results.

  3. Thanks for posting this, Adam.
    In a hybrid configuration, it is possible to identify active onprem mailboxes? I think we’ve migrated all the mailboxes to O365 that are going to remain active. I would like to tell if there are any active onprem mailboxes that have been missed and migrate them.
    Thanks!

    • avatar
      Adam the 32-bit Aardvark says:

      Hi Joe,
      Generating the list of active mailboxes is the easy part – you can follow the instructions from the article above. The slightly harder part is comparing the list from the on-prem Exchange with the list of mailboxes in Office 365. Personally, I would do the following:

      1. generate the two lists mentioned above and export them to CSV files,
      2. open them in MS Excel, leave only mailbox aliases so that they are displayed in two different columns,
      3. and use Conditional Formatting > Highlight Cells Rules > Duplicate Values… to mark unique values.

      Then, all mailboxes which have not been migrated to Office 365 will be highlighted.
      Hope this helps

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

CodeTwo sp. z o.o. sp. k. is a controller of your personal data.
See our Privacy Policy to learn more.