This page looks best with JavaScript enabled

Auto Create Exchange Mailboxes v1.8

 ·  β˜• 4 min read

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 Gallery, PowerShell Gallery and GitHub.

If you have any questions or comments, please leave them below.

Please consider donating to support my work:

-Mike

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 For SMTP Authentication

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.

1
2
$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

Here’s a list of all the command line switches and example configurations.

1
-OU

The AD Organisational Unit (including child OUs) that contains the users to create Exchange Mailboxes for.

1
-Datab

The Exchange database to create the mailboxes in. If you do not configure a Database, the smallest database will be used.

1
-RP

The retention policy that should be applied to the users.

1
-Compat

Use this switch if you are using Exchange 2010.

1
-L

The path to output the log file to. The file name will be “Create-Mailboxes.log”

1
-Subject

The email subject that the email should have. Encapsulate with single or double quotes.

1
-SendTo

The e-mail address the log should be sent to.

1
-From

The from address the log should be sent from.

1
-Smtp

The DNS name or IP address of the SMTP server.

1
-User

The user account to connect to the SMTP server.

1
-Pwd

The txt file containing the encrypted password for the user account.

1
-UseSsl

Connect to the SMTP server using SSL.

Example

1
Create-Mailboxes.ps1 -Ou "OU=NewUsers,OU=Dept,DC=contoso,DC=com" -Datab "Mail DB 2" -Rp "1-Month-Deleted-Items" -L C:\scripts\logs -Subject 'Server: Created Mailboxes' -Sendto [email protected] -From [email protected] -Smtp smtp-mail.outlook.com -User [email protected] -Pwd P@ssw0rd -UseSsl

This will create mailboxes for users that do not already have one in the OU NewUsers and all child OUs. It will create the mailbox using Mail DB 2 and apply the retention policy “1-Month-Deleted-Items”. If you do not configure a database, the smallest database will be used. A log will be output to C:\scripts\logs and e-mailed with a custom subject line, using a secure connection.

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.
Share on
Support the author with