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.

1

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.

2

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:

3
4

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:

5

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

Contact Us

On Key

More Posts

Mastering Azure AD Connect - A Comprehensive Guide by WME
Active Directory

Mastering Azure AD Connect – A Comprehensive Guide

Modern businesses are fast moving toward cloud-based infrastructure. In fact, cloud-based business is not just a trend anymore but a strategic necessity. Microsoft’s Azure Active Directory (Azure AD) has become a frontrunner in this domain. It

Read More »
Security Best Practices in SharePoint
Office 365

Security Best Practices in SharePoint

Microsoft SharePoint is an online collaboration platform that integrates with Microsoft Office. You can use it to store, organize, share, and access information online. SharePoint enables collaboration and content management and ultimately allows your teams to

Read More »
The Ultimate Guide to Microsoft Intune - Article by WME
Active Directory

The Ultimate Guide to Microsoft Intune

The corporate world is evolving fast. And with that, mobile devices are spreading everywhere. As we venture into the year 2024, they have already claimed a substantial 55% share of the total corporate device ecosystem. You

Read More »
Protecting Microsoft 365 from on-Premises Attacks
Cloud Security

How to Protect Microsoft 365 from On-Premises Attacks?

Microsoft 365 is diverse enough to enrich the capabilities of many types of private businesses. It complements users, applications, networks, devices, and whatnot. However, Microsoft 365 cybersecurity is often compromised and there are countless ways that

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.