Create Exchange 2013 Mailboxes in Bulk

The post will provide a script that will create Exchange 2013 mailboxes in bulk by either supplying a file of usernames, or usernames on the command line. This script will only work with Exchange 2013. You must either run this script directly on the Exchange server, or have PowerShell remote session rules enabled. You also have to have the ability to create mailboxes on the server.

When you first copy or download the script, be sure to input the name of your exchange server on line 32. This must be the “real” DNS name; a CNAME will not work. Be sure to leave the quotes around the name.

Next, be sure to change the AD search base on line 51. Again, make sure that you keep the quotes. If you do not want to limit your search, you can remove ‘-searchbase “ou=employees,dc=contoso,dc=com”‘ from the line. Keep in mind that the more records you bring in, the longer the script will take to execute.

Script Actions

The script does three things. The first thing it does is take the usernames from either the file or the command line and check them against Active Directory to ensure that they exist. If they do not, the script essentially throws them out to another variable and does not try to enable the mailbox. This variable is displayed later, so that you can see which ones do not exist. You can use this information to confirm the names to ensure that no mistake has been made.

Secondly, the script checks to ensure that the validated usernames do not already have a mailbox. Just as in step one, if the mailbox already exists, it throws those usernames out to another variable and does not try to enable them again. This variable is again displayed later so that you know exactly what happens.

Third, if the username is valid and the mailbox does not exist, the script enables the mailbox. You will again get a list of those users that were actually enabled.

Because of the active directory and mailbox checks, the script takes anywhere from 20 to 30 seconds to execute.

Executing the Script

There are two available parameters for you to specify: file and usernames. There is also a help section that can be viewed by typing “get-help .\enable-exchange-mailboxes”. Here is how we add one or multiple users by typing them from the PowerShell shell:

Notice that for one user, the parameter is still “usernames” and that if specifying multiple users, separate them with a comma.

If you want to use a file, specify it like this:

This file can contain as many usernames as you wish. You can only specify one file at a time.

I hope this script can improve your Exchange environment management. Here’s the entire script:

################################################

###                                         ###

###     Builds Exchange mailboxes           ###

###                                         ###

###    Change variable $exchange_ser to     ###

###    match name of your Exchange server   ###

###                                         ###

###    Change AD search base in line 51     ###

###                                         ###

################################################

# help section

<#

.SYNOPSIS

Enable mailboxes using username(s) or a file of usernames.

.PARAMETER File

Reference a file of usernames to enable.

.PARAMETER usernames

Reference one or more usernames to add.

.EXAMPLE

.\enable-mailbox.ps1 -usernames “username1,username2”

.EXAMPLE

.\enable-mailbox.ps1 -file “C:\usernames.txt”

#>

# define parameters

PARAM (

[string]$file,

[string]$usernames

)

$exchange_ser = “<server name>”

# take input from parameters and format it

$enable_mailboxes = @()

If ($file -ne “”) { $enable_mailboxes = get-content $file }

If ($usernames -ne “”) { $enable_mailboxes = $usernames.split(“,”) }

If (($file -eq “”) -and ($usernames -eq “”)) {

write-host “”

write-host “No parameters specified.” -ForegroundColor Red

write-host ‘Use “-file <path to file>” or “-usernames <usernames sperated by commas>”‘ -ForegroundColor Red

write-host “”

break }

write-host “” # line break

write-host “Validating User Accounts…” -ForegroundColor Green

# active directory check for user accounts

import-module activedirectory

$all_users = @()

$all_users += (get-aduser -filter * -searchbase “ou=employees,dc=contoso,dc=com” -ResultSetSize $null).SamAccountName

$f_enable_mailboxes = @()

$no_useracc = @()

ForEach ($enable_mailbox in $enable_mailboxes) {

If (($all_users -contains $enable_mailbox) -eq $true) { $f_enable_mailboxes += $enable_mailbox }

Else { $no_useracc += $enable_mailbox }

}

write-host “”

write-host “Enabling Mailboxes…” -ForegroundColor Green

# create powershell session to exchange server

$ps_session = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri https://$exchange_ser/powershell

$a = Import-PSSession -session $ps_session -DisableNameChecking

# get current mailboxes

$cur_mailboxes = (get-mailbox -resultsize unlimited).Name

$exists = @()

$created = @()

# creates mailbox if it does not already exist

ForEach ($f_enable_mailbox in $f_enable_mailboxes) {

If (($cur_mailboxes -contains $f_enable_mailbox) -eq $true) { $exists += $f_enable_mailbox }

Else { enable-mailbox $f_enable_mailbox }

}

write-host “”

# display results

write-host “Already Existed:” -ForegroundColor Red

ForEach ($name in $exists) { write-host $name -ForegroundColor Red}

write-host “”

write-host “No User Account Found:” -ForegroundColor Red

ForEach ($name in $no_useracc) { write-host $name -ForegroundColor Red }

# remove PowerShell session

remove-pssession $ps_session

Disclaimer

All content provided on this blog is for information purposes only. Windows Management Experts, Inc makes no representation as to accuracy or completeness of any information on this site. Windows Management Experts, Inc will not be liable for any errors or omission in this information nor for the availability of this information. It is highly recommended that you consult one of our technical consultants, should you need any further assistant.

Share:

Facebook
Twitter
LinkedIn
Picture of Matt Tinney

Matt Tinney

Professional IT executive & business leader having decades of experience with Microsoft technologies delivering modern-day cloud & security solutions.

Contact Us

=
On Key

More Posts

WME Cybersecurity Briefings No. 020
Cyber Security

WME Security Briefing 26 July 2024

Pro-Houthi Group Targets Yemen Aid Organizations with Android Spyware Overview A suspected pro-Houthi group, OilAlpha, is targeting humanitarian organizations in Yemen with advanced Android spyware. The operation is associated with the activity cluster codenamed OilAlpha. It

Read More »
WME Cybersecurity Briefings No. 019
Cyber Security

WME Security Briefing 23 July 2024

Samba File Shares Targeted by DarkGate Malware in Recent Cyber Offensive Overview Recent investigations by Palo Alto Networks uncover a brief but significant cyberattack campaign utilizing DarkGate malware. This malicious software exploited Samba file shares to

Read More »
WME Cybersecurity Briefings No. 018
Cyber Security

WME Security Briefing 15 July 2024

OVHcloud Mitigates Record-Breaking 840 Million PPS DDoS Attack Overview In April 2024, OVHcloud, a top French cloud computing firm, successfully stopped a massive DDoS attack. The attack hit a record-breaking rate of 840 million packets per second

Read More »
E-Commerce Security - Solutions for Online Retailers
Azure

E-commerce Security – Solutions for Online Retailers

Today’s hyper-charged e-commerce landscape demands top-notch cybersecurity measures. Cybersecurity for this bustling sector isn’t just about ticking a technical box; it’s the cornerstone of building trust. As businesses and consumers flock to the online space, the

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.

=