Introduction
This script is used to help administrators to keep an unlicensed account's mailbox address or contents.
Scenarios
When an account won't be used anymore, the license would be removed for this account. But the mailbox's address or contents should be kept for further reference.
Prerequisites
This script requires Windows Azure Active Directory Module for Windows PowerShell. Please download and install the package. This script cannot work correctly without this package.
Script
You can use this script in the following way.
1. Open Windows PowerShell.
2. Type the command< Script Path> at the Windows PowerShell Console.
For example, type C:\Script\ RemoveorArchiveUnusedMailbox.ps1
Here are some code snippets for your reference.
Set-Mailbox -Identity $Mailbox -EmailAddresses $DisableAddress Set-MsolUserLicense -UserPrincipalName $BoxInfo.PrimiarySmtpAddress -RemoveLicenses $MsolUser.Licenses.AccountSkuId Set-MsolUserPrincipalName -UserPrincipalName $mailbox -NewUserPrincipalName $DisableAddress New-DistributionGroup -Name "Removed Mailbox $($BoxInfo.Alias)" -PrimarySmtpAddress $BoxInfo.PrimarySmtpAddress Set-DistributionGroup -Name "Removed Mailbox $($BoxInfo.Alias)" -EmailAddresses $BoxInfo.EmailAddresses
Set-Mailbox -Identity $Mailbox -EmailAddresses $DisableAddress Set-MsolUserLicense -UserPrincipalName $BoxInfo.PrimiarySmtpAddress -RemoveLicenses $MsolUser.Licenses.AccountSkuId Set-MsolUserPrincipalName -UserPrincipalName $mailbox -NewUserPrincipalName $DisableAddress New-DistributionGroup -Name "Removed Mailbox $($BoxInfo.Alias)" -PrimarySmtpAddress $BoxInfo.PrimarySmtpAddress Set-DistributionGroup -Name "Removed Mailbox $($BoxInfo.Alias)" -EmailAddresses $BoxInfo.EmailAddresses
Examples
Example: convert the mailbox to a shared mailbox.
The sample is shown below.
RemoveorArchiveUnusedMailbox.ps1 -Credential $credential -Mailbox UnlicensedUser@contoso.com
Command Screenshot:
Result Screenshot:
The mailbox will be converted to shared mailbox when the mailbox is under 10GB in size.
Otherwise, the administrator will be assigned the full access of this mailbox to export.
Example: convert the mailbox to a shared mailbox
The sample is shown below
RemoveorArchiveUnusedMailbox.ps1 -Credential $credential -Mailbox UnlicensedUser@contoso.com –Notkeeping
Command Screenshot:
Result:
A new Distribution Group using the same SMTP address will be created.
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.