Office 365 – Script to Create New Primary and Archive Mailbox

Script can be downloaded by clicking this Link. Rename the file to PS1 before it can be used.

Please note that once the mailbox has been created, it needs to be activated.

In order to create primary and archive mailboxes in Office 365, the following script can be used.

  • Please note that this script will require an input file.
  • The content of the file should be the SAMAccountName of the newly created users in Active Directory.
  • As an example, all new users being created have a logon name of firstname.lastname – this is referred to as the SAMAccountName (without the domain portion). The file should contain entries like – each name on its own like:

Clark.Kent
Bruce.Wayne
Bruce.Banner

 

##########################################################################
# This function is used to create a “pause” in the script.
# Continuation of the script requires an input from the keyboard.
# Pauses have been added at different steps of the script but can be safely removed.
##########################################################################

 ######
# AUTHOR: IBRAHIM U. BENNA
######

 FunctionPause ($Message=”Press Any Key to Continue…”)
{
Write-host $Message
$Null=$host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
Write-host ” ”
}
Write-host -foregroundcolor Red “Please make sure you are running this script from the Microsoft Exchange Management Shell on a machine used to manage the Exchange organization”

Echo ” ”

Pause

#Import Users to be mailbox-enabled from text file. If no text file input is provided, the script immediately terminates. The file must contain the logon names of the users being activated

If ($Args.count -eq 0) {
Write-host -foregroundcolor Red “You need to specify a file name in the command. The file should contain a list of users to be activate.”

echo ” ”

Write-host -foregroundcolor Red “Please re-run the script providing the file name using the syntax ‘New-O365Mailbox.PS1 InputFileName.txt’ ”

echo ” ”

Exit
}

#Enabling mailbox in the cloud – This section will read the content of the text file

$Users=get-content $Args[0]

Foreach ($U in $Users) {
echo ” ”

#Configuring the TargetAddress attribute on the user account in Active Directory and then enabling the mailbox in Exchange Online.
Write-host -foregroundcolor DarkYellow “Enabling remote mailbox for $u”

echo ” ”

Write-host –foregroundcolor Red “MAKE SURE YOU HAVE CHANGED THE TENANT DOMAIN IN THE SCRIPT!”

Pause

#Configuring the TargetAddress attribute on the user account in Active Directory and enabling primary mailbox for user
$routing=$u+“@TENANTNAME.mail.onmicrosoft.com
enable-remotemailbox $u -remoteroutingaddress $routing
Write-Host -foregroundcolor Yellow “$U has O365 primary mailbox enabled”

echo ” ”

#Configuring and enabling an archive mailbox for the user
Write-host -foregroundcolor DarkYellow “Enabling archive mailbox for $u”
enable-remotemailbox $u -archive
Write-Host -foregroundcolor Yellow “$U has archive mailbox enabled”
echo ” ”
}

Scripts for Creating Archive Mailboxes in Exchange 2010

Background Information

I had a client who has been running Exchange 2010 SP1 for some time and they have a little over 7,000 user mailboxes in their environment. Their environment consisted of 2 Exchange servers running the mailbox role and the user mailboxes were spread between the two servers. Each mailbox server had 12 mailbox databases (totalling 24) and it got to a point that they wanted to implement archiving for their users.

The client had already created 12 additional mailbox databases to host the archive mailboxes (these databases were placed on slower disks) so in total each server now has 24 databases (totalling 48 in the environment).

Client Requirements

The client wants to create mailboxes on the same server as the primary mailbox for each user mailbox. For example,  if USER2’s mailbox is on ExchangeSRV02 on database DB09, then this script will create an archive mailbox for USER2 on archive database ARCHIVEDB09

SCRIPT 1 – Create Archive for ALL mailboxes (Save this file as FileName.PS1)

$users = Get-Mailbox -RecipientTypeDetails usermailbox

foreach ($u in $users) {

$MailboxDatabase= (Get-Mailbox $U).Database

$ArchiveDatabase= “Archive”+ ( $MailboxDatabase.Name).Substring( 0)

Enable-Mailbox $U -Archive -ArchiveDatabase $ArchiveDatabase

write-host “Done processing $u…”

}

In an Exchange Powershell, simply run the script as FileName.PS1

SCRIPT 2 – Create Archive for selected mailboxes (Save this file as FileName.PS1)

As a modification to this script – if you have a list of names for the users who you want to have archive mailboxes, you can place these names in a text file (names.txt for example)

If ($Args.Count -eq 0) {
write-host “You need to specify a file with a list of users as a parameter at the command line!!!”
Exit
}
$users = get-content $args[0]
foreach ($u in $users) {
$MailboxDatabase= (Get-Mailbox $U).Database
$ArchiveDatabase= “Archive”+ ( $MailboxDatabase.Name).Substring( 0)
Enable-Mailbox $U -Archive -ArchiveDatabase $ArchiveDatabase
write-host “Done processing $u…”
}

In an Exchange Powershell, simply run the script as FileName.PS1 Names.txt