HowSoftwareTech

Mailbox Creation Script for Exchange Server

the Powershell is a very powerful programming language for performing mass operations such as creating mailboxes on an Exchange server.

For one client, hundreds of boxes had to be created and it was unthinkable to create the boxes by hand. Indeed the task is time consuming and takes time!

To help you, complete tutorial to create your Exchange mailboxes using a Powershell script.

Powershell script to create mailboxes on Exchange

About scripts

The scripts offered in this tutorial are compatible with Exchange 2013/2016/2019 and Exchange Online (Microsoft 365).

Scripts can use a CSV file so that they can read multiple lines and create the boxes in the chain.

powershell ise
Here is an example of Ise Powershell code

Powershell and Operation scripts

The CSV file

The scripts presented above rely on .csv files delimited by a semicolon (;). The Powershell script loads the file then traverses the lines to fill in the various fields in the Active Directory in order to create the mailboxes.

Here is an example of a CSV file:

example csv file creation mailbox
Csv file opened with notepad

The .csv file delimits the login, first name and last name fields, the delimiter is indeed the semicolon. Fields can be added in the CSV file

You can also use LibreOffice or Excel to create a CSV file

Excel csv file example

The foreach loop

Once the file is loaded it is necessary in the powershell script to define a loop which for each element of the file will perform an action (modification / creation / deletion)

Script to create Exchange mailboxes.

#On ajoute les fonctionnalités Exchange Management Shell
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; 
#On import le fichier CSV
Import-Csv -Path "C:Sourcesmailbox.csv" -Delimiter ";" -Encoding Default | foreach {
 
Enable-Mailbox -Identity $_.login -Database "Base-exchange"
 }

Here for each line of the CSV file we will create an Exchange mailbox (Enable-MailBox) for a user already created in the Active Directory. Note that the search is based on the “login” field of the csv file. ($ .login).

To create a new account and a new box you must use the “New-Mailbox” command

New-Mailbox -Name "Tutos-Informatique" -UserPrincipalName [email protected] -Password (ConvertTo-SecureString -String 'Pa$$word1' -AsPlainText -Force) -FirstName Tutos -LastName Informatique

You can add fields in your csv file like first and last name and email address. Your script will have to adapt here is an example

#On ajoute les fonctionnalités Exchange Management Shell
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; 
#On import le fichier CSV
Import-Csv -Path "C:Sourcesmailbox.csv" -Delimiter ";" -Encoding Default | foreach {
 
New-Mailbox -Name $_.nom -UserPrincipalName $_.adressemail -Password (ConvertTo-SecureString -String 'Pa$$word1' -AsPlainText -Force) -FirstName $_.Nom -LastName $._prenom  -Database "Base-exchange"
 }

Now you know more about the letterbox creation;) Do not hesitate to consult very detailed Microsoft documentation

Developers are welcome to submit scripts in the comment portion of this article.

  • PowerShell Core and Windows PowerShell – Language Fundamentals (2nd Edition)

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker