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 migrate from Exchange Server 2016/2019 to Microsoft 365

How to migrate from Exchange Server 2016/2019 to Microsoft 365

Migrating Exchange data to the cloud is not rocket science – explore your options and launch the migration stress-free. As you may already know, Exchange Server 2016 and 2019 have reached end of life and are no longer officially supported by Microsoft. If your organization still uses either of these platforms to manage email, contacts, calendars, and tasks, keep in mind that: Your environment may become vulnerable to newly discovered security threats – Microsoft will no longer provide security updates for Exchange 2016 and 2019 (unless you’ve enrolled in the Extended Security Update program, which ends in October 2026). Your emails may get blocked – Microsoft has started to throttle and block emails sent from unsupported Exchange Server versions to Exchange Online (as I covered in this article). That said, migrating to a supported platform is now the only viable long-term option for keeping your organization’s email environment secure, supported, and fully operational. If your organization wants or needs to keep things on‑premises (and continue using Microsoft’s solutions for that), upgrading to Exchange Server Subscription Edition (SE) is the only path forward. But given Microsoft’s clear preference for its cloud services – evident in the faster rollout of new features and the many security capabilities available exclusively in Microsoft 365 (Office 365) – now is a great time to leave your on‑prem environment behind and migrate to Exchange Online as part of Microsoft 365. While switching over to a new platform might seem like a rough ride, I’ll show you some easy ways to follow when migrating mailboxes from Exchange Server 2016/2019 to Microsoft 365. How to prepare for email migration to Microsoft 365 Before you start the migration process, you need to make sure your environment is ready for the move. For this purpose, you can use this guide in the Microsoft 365 admin center – it will help you connect your organization to Microsoft 365 and integrate your existing user accounts with Microsoft Entra ID. Microsoft also recommends completing the steps below: Set up an SPF record to determine valid email sources for your organization’s Microsoft 365 domain. Set up the Exchange Online Protection service as a means of protection against spam and malware. If you’re behind on updates, make sure to install the latest Cumulative Update (CU). And here is my quick, less obvious Microsoft 365 migration checklist: Verify if your software will work in Microsoft 365 (especially when it comes to server software). Microsoft 365 migration might be the time you learn that there is crucial legacy software that half the company uses and which is hard to replace. Encourage the whole company to clean up projects. It’s much easier to do this before the migration and start fresh. Gather as much information about your on‑premises environment as possible. For example, you might need to recreate access roles and permissions from scratch in the cloud or set up mail flow rules. Without prior research, it will be much more difficult. Verify if you need to migrate service accounts. There can be a lot of them on‑premises and in most cases, you won’t need them after the move. Review mailbox size limits in Exchange Online before migration to see which licenses you’ll need and whic
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.
How to start remote PowerShell session to Exchange or Microsoft 365

How to start remote PowerShell session to Exchange or Microsoft 365

One of many features of the PowerShell command line tool is its ability to connect with and manage the Exchange Server remotely. The procedure described below applies to the classic on-prem Exchange server and to the Microsoft 365/Exchange Online version.

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.