Tag Archives: Exchange

new-mailbox.ps1

A little while ago we decided to reorganize our mailbox databases. This new scheme no longer classifies users into department or employee type. We decided to move towards a balanced database structure to help keep all our edb files about the same size.

We are now using the following script to create a new mailbox within the smallest database with the most whitespace where the DB names are as follows.
DB01
DB02
DB03

If the DB name also ends with ‘ (Unlimited)’ than we will choose that for mailboxes that do not have limits.

# new-mailbox.ps1 -Identity [string] -Alias [string] -DisplayName [string] -Unlimited

param{
	[string]$Identity,
	[string]$Alias,
	[string]$DisplayName,
	[switch]$Unlimited
}

# get mailbox database statistics.
$dbs = Get-MailboxDatabase -Status | Select-Object @{E={$_.Identity};L="Name"},@{E={(Get-Item ("\\" + $_.MountedOnServer + "\" + ($_.EdbFilePath -replace ":","$"))).Length / 1048576};L="EdbFileSizeMB"},@{E={$_.AvailableNewMailboxSpace.ToMB()};L="EdbWhiteSpaceMB"} | Sort-Object -Property @{E="EdbFileSizeMB";D=$false},@{E="EdbWhiteSpaceMB";D=$true}

switch ($Unlimited) { 
	$true { $filter = "(Unlimited)" }
	default { $filter = "DB[0-9]{1,4}$" } 
}

# get the smallest database with the most whitespace
$db = ($dbs | ?{$_.Name -match $filter})[0].Name.Name

Enable-Mailbox -Identity "DOMAIN\$identity" -Alias $alias -Database $db -DisplayName $displayname

Set-Mailbox -EmailAddresses @{add=($identity + "@domain.com")}