Script: Set-MailboxPolicies.ps1


7/8/13 Update: Added new script: Create-Mailbox.ps1

2/26/13 Update: Added support for checking archive mailbox quota limits and setting them. Get latest version here. Code below has NOT been updated.

When new mailboxes are created or migrated to Exchange 2010 archiving and retention policies and Single Item Recovery are not set. While these can be set with cmdlet Extension Agents when creating new mailboxes or when running the New-MoveRequest cmdlet they are a bit advanced to setup. So I created a script to be run, via a Scheduled Task mainly, that can check for mailboxes that don’t have the desired policies set and set them. I also posted previously on cmdlet Extensions back in 2010 here: https://blog.jasonsherry.net/2010/10/22/exchange2010_cmdlet_extension_agents/

The script has two key variables\parameters: SetSingleItemRecovery & SetRetentionPolicy. If both are set to $True then it will search for all users that don’t have either of these set. If you just want to find users that have just one of these values not set, then set the other variable to $False.  As a safety measure the script will default to just showing what mailboxes would have been updated. Changed $MakeChanges = $True to have the script actually make changes.

This script doesn’t have error handling and is a quick and fairly simple script. As always, goto my website and get the latest version since I may forget to update the code in this post.

Source: http://izzy.org/scripts/Exchange/Admin/Set-MailboxPolicies.ps1

# This script will check if certain settings are enabled on a mailbox and set them

# Created by Jason Sherry (izzy@izzy.org) 2/19/2013
# Last Updated:2/19/2013

# Source: http://izzy.org/scripts/Exchange/Admin/Set-MailboxPolicies.ps1

# If CustomAttribute10 = 'Ignore' mailbox will be skipped. This should be set on system
# and other mailboxes that you don't want policies set on.

# If a user is not specified a query is ran to find all users that don't have the policies set

Param(
	[String]$User,
	[Boolean]$SetSingleItemRecovery = $True,
	[Boolean]$SetRetentionPolicy = $True)

$MakeChanges = $False
$RetentionPolicy = 'Default Archive and Retention Policy'

Function ProcessMailbox ($Mailbox) {
	$MailboxName = $Mailbox.Name
	Write-Host "Processing mailbox: $MailboxName" -ForegroundColor Cyan

	If ($SetSingleItemRecovery -And $SetRetentionPolicy) {
		Write-Host "`tSetting RetentionPolicy = [$RetentionPolicy] and enabling Single Item Recovery" -ForegroundColor Green
		If ($MakeChanges) {Set-Mailbox $Mailbox -RetentionPolicy $RetentionPolicy –SingleItemRecoveryEnabled $true}
	}
	ElseIf ($SetSingleItemRecovery -And !($SetRetentionPolicy)) {
		Write-Host "`tEnabling Single Item Recovery"
		If ($MakeChanges) {Set-Mailbox $Mailbox –SingleItemRecoveryEnabled $true}
	}
	Else {
		Write-Host "`tSetting RetentionPolicy = $RetentionPolicy"
		If ($MakeChanges) {Set-Mailbox $Mailbox -RetentionPolicy $RetentionPolicy}
	}
}

If ($User -eq "") {
	Write-Host "`nGetting mailboxes" -ForegroundColor Green
	If ($SetSingleItemRecovery -And $SetRetentionPolicy) {
		$Mailboxes = Get-Mailbox -ResultSize Unlimited | ? {($_.SingleItemRecoveryEnabled -ne $True -Or $_.RetentionPolicy -eq $Null) -And $_.CustomAttribute10 -ne "Ignore" -And $_.ExchangeVersion -Like "*14.*"}
	}
	ElseIf ($SetSingleItemRecovery) {
		$Mailboxes = Get-Mailbox -ResultSize Unlimited | ? {$_.SingleItemRecoveryEnabled -ne $True -And $_.CustomAttribute10 -ne "Ignore" -And $_.ExchangeVersion -Like "*14.*"}
	}
	Else {
		$Mailboxes = Get-Mailbox -ResultSize Unlimited | ? {($_.SingleItemRecoveryEnabled -ne $True -Or $_.RetentionPolicy -eq $Null) -And $_.CustomAttribute10 -ne "Ignore" -And $_.ExchangeVersion -Like "*14.*"}
	}

	$Count = $Mailboxes.Count
	If ($Count -eq $Null -and $Mailboxes -ne $Null) {$Count = 1}

	Write-Host "`Found [$Count] mailboxes`n"  -ForegroundColor Cyan
	ForEach ($Mailbox in $Mailboxes) {
		ProcessMailbox $Mailbox
	}
}
Else {
	$Mailbox = Get-Mailbox $User -ErrorAction SilentlyContinue
	If ($Mailbox -eq $Null) {
		Write-Host "`nMailbox not found for: $User`n" -ForegroundColor Red
		Exit
	}
	If ($Mailbox.ExchangeVersion -NotLike "*14.*") {
		Write-Host "`nMailbox [$Mailbox] is not hosted on an Exchange 2010 server`n" -ForegroundColor Yellow
	}
	Else {
		ProcessMailbox $Mailbox
	}
}

About Jason Sherry

I am a ~30 year Exchange consultant and expert. I currently work for Commvault as a Solutions Specialist for Microsoft Infrastructure For more info see my resume at: http://resume.jasonsherry.org
This entry was posted in Exchange, Microsoft, Script and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s