Auto Create Exchange Mailboxes v1.8

I needed to write a PowerShell utility to create Exchange mailboxes for new users in Active Directory. The utility needed to create the mailboxes, use different databases, retention policies, and take users from several Organisational Units along with users in child OUs.

This utility is available to download from the Microsoft TechNet GalleryPowerShell Gallery and GitHub.

 

Features and Requirements

  • This script is designed to be run locally on an Exchange Server.
  • The Exchange Server management tools must be installed.
  • The Active Directory management tools must be installed.

This script has been tested on Windows Server 2016 with Exchange 2016, Windows Server 2012 R2 with Exchange 2013, and Windows Server 2008 R2 with Exchange 2010.

 

The -file Parameter And Why It Matters

When running a PowerShell script from the command prompt or as a Scheduled Task without the -file parameter, any user defined parameters which contain spaces, must be surrounded by ‘single quotes’ as using “double quotes” results in the script not seeing the parameter as a complete string. When running a PowerShell script with the -file parameter, the “double quotes” work as expected. Cue “The More You Know” GIF.

With this in mind, when running the script please use the -file parameter and surround any database & retention policy friendly names “double quotes” if they have spaces. Also the Distinguished Name for the OU you wish to query for users should be surrounded by “double quotes” because PowerShell will remove the comma’s from the string.

 

Generating A Password File

The password used for SMTP server authentication must be in an encrypted text file. To generate the password file, run the following command in PowerShell, on the computer that is going to run the script and logged in with the user that will be running the script. When you run the command you will be prompted for a username and password. Enter the username and password you want to use to authenticate to your SMTP server.

Please note: This is only required if you need to authenticate to the SMTP server when send the log via e-mail.

$creds = Get-Credential
$creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt

After running the commands, you will have a text file containing the encrypted password. When configuring the -Pwd switch enter the path and file name of this file.

 

Configuration

The table below shows all the command line options available with descriptions and example configurations.

Command Line Switch Mandatory Description Example
-Ou Yes Enter the DN (Distinguished Name) of the OU that you wish to search for users. The DN must be surrounded by “double quotes.” “ou=Staff,dc=contoso,dc=com”
-Datab No Enter the friendly name of the database you wish to create the mailboxes in. If the name has spaces, please surround it with “double quotes”. If a database is not configured the script will use the smallest one available. Mail-DB-1 OR “Mail DB 01”
-Rp No If you wish to set a retention policy for the new mailboxes, specify the friendly name here. As with the database, if the name has spaces surround it with “double quotes”. Retention-Policy-01 OR “Retention Policy 01”
-L No Location to store the optional log file. The name of the log file is automatically generated. C:\foo
-Subject No The subject line that the email should have. Encapsulate with single or double quotes. ‘Server: Notification’
-SendTo No The email address to send the log file to. me@contoso.com
-From No* The email address that the log file should be sent from.

*This switch isn’t mandatory but is required if you wish to email the log file.

example@contoso.com
-Smtp No* SMTP server address to use for the email functionality.

*This switch isn’t mandatory but is required if you wish to email the log file.

mail01.contoso.com

OR

smtp.live.com

OR

smtp.office365.com

-User No* The username of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

example@contoso.com
-Pwd No* The location of the file containing the encrypted password of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on your SMTP server.

c:\foo\ps-script-pwd.txt
-UseSsl No* Add this option if you wish to use SSL with the configured SMTP server.

Tip: If you wish to send email to outlook.com or office365.com you will need this.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

N/A

 

Change Log

2019-09-04 v1.8

  • Added custom subject line for e-mail.

2018-01-13 v1.7

  • Fixed an issue where the script would not load the Exchange PowerShell snap-in and PowerShell would unexpectedly quit.
  • Added the capability for the script to create mailboxes in the smallest database available.
  • The ability to configure a specific database if required remains.

2017-10-16 v1.6

  • Changed SMTP authentication to require an encrypted password file.
  • Added instructions on how to generate an encrypted password file.

2017-10-07 v1.5

  • Added necessary information to add the script to the PowerShell Gallery.

2017-09-27 v1.4

  • Added improved, cleaned logging. Log output is no longer handled by PowerShell’s Transcript functionality.
  • Cleaned up code formatting.

2017-07-22 v1.3

  • Improved commenting on the code for documentation purposes.
  • Added authentication and SSL options for e-mail notification.

 

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 )

Google photo

You are commenting using your Google 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: