Many times as Exchange Admins, we are tasked to find out if we have licenses available for new staff or we are running out of space on a server or simply just performing a cleanup and we need to know which users are inactive. In a small company this will be easier to achieve but if you dealing with thousands of mailboxes, then you need PowerShell to help you out a bit.

I have a script that searches through all the mailboxes and will display information based on what you set the Inactive date to. On line 5 of the script, you can modify the days from 90 to 30 or 60 or whichever day you require:

$inactiveDate = (Get-Date).AddDays(-90)  # In this example, 90 days of inactivity

The script has the Exchange Snapin added on line 2 incase you need to run this from another server.

Below is the output from Exchange 2019:

Quickly find inactive mailboxes in exchange 2019 by using powershell.

If you run it from PowerShell ISE, this will be the output, same as the above, just within PowerShell:

Quickly find inactive mailboxes in exchange 2019 by using powershell.

Script

Below is the script, you can save it to your Exchange server as a PS1 script file or run it from PowerShell ISE:

# Import the Exchange module
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

# Set the date threshold for inactivity (adjust as needed)
$inactiveDate = (Get-Date).AddDays(-90)  # In this example, 90 days of inactivity

# Get all mailboxes and their statistics
$mailboxes = Get-Mailbox -ResultSize Unlimited
$inactiveMailboxes = @()

foreach ($mailbox in $mailboxes) {
    $statistics = Get-MailboxStatistics -Identity $mailbox.DistinguishedName

    # Check the last logon time
    if ($statistics.LastLogonTime -lt $inactiveDate) {
        $inactiveMailbox = [PSCustomObject]@{
            DisplayName   = $mailbox.DisplayName
            Alias         = $mailbox.Alias
            LastLogonTime = $statistics.LastLogonTime
        }
        $inactiveMailboxes += $inactiveMailbox
    }
}

# Display the results
$inactiveMailboxes | Format-Table -AutoSize

In the script I just display the information on the screen but you can modify the last line to export it to CSV if you need to.

Hope it helps.

Discover more from Everything-PowerShell

Subscribe now to keep reading and get access to the full archive.

Continue reading