Disable Inactive Mailbox

Introduction

This script will convert an existed mailbox to a new mail user.

Scenarios

This script is to help Exchange Administrators to convert mailboxes to mail users via PowerShell.

Script

This script contains one advanced function Convert-OSCMailboxtoMailUser, you can use this script in the following ways:

Step1: Run the script in the Exchange Management Shell, type the one command: Import-Module <Script Path> at the prompt.

For example, type Import-Module C:\Script\ConvertMailboxtoMailUser.psm1

Here are some code snippets for your references.

PowerShell
Edit|Remove
if($Mbxs){
    Foreach($mbx in $Mbxs){
        $MbxFolderInfo = Get-MailboxFolderStatistics $mbx -IncludeOldestAndNewestItems -FolderScope SentItems
        $lastreceiveddate = $MbxFolderInfo.NewestItemReceivedDate
        #check lastreceivedate
        if($lastreceiveddate){
            $durationdays = (New-TimeSpan $lastreceiveddate.ToLocalTime()).TotalDays
            if($durationdays -ge $Days){
                #Disable the Mailbox
                Disable-Mailbox $mbx -confirm:$false
                Write-Host "Mailbox $mbx is disabled."
            }
        }
    }
}

Examples

Example 1: Disable all mailboxes which were inactive in 90 days in Exchange Environment.
Command: Disable-OSCInactiveMailbox -Days 5
Screenshot:

Example 2: Disable all mailboxes on Mailbox Server "MailboxServer1" which were inactive in 5 days.
Command: Disable-OSCInactiveMailbox -Days 5 -Server "MailboxServer1"
Screenshot:
 

Prerequisite

Exchange 2010 and above Exchange Version
English Version only
PowerShell 2.0

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.