Creating bulk recipients on a individual bases can be time consuming and often leads to inaccuracies. Developing user attributes in Microsoft Excel for creating and modification mailboxes can save time and ensure that the creation of the recipients is mirrored and accurate.  In this blog we will review the use of Import-CSV to create, modify, and manage recipients.

In addition to Exchange Management Shell, it is recommended to use Microsoft Excel to create a CSV file. If you don’t have access to Excel, you can use notepad to edit the CSV file.

How to do it

  1. Using a CSV file, mailboxes will be created in bulk using the parameters, row 1 of the image below, and the value for each parameter

  1. Go to File | Save As and select CSV (Comma delimited)(*.csv) for the file type. Save the file as C:Mailboxs.CSV:
  2. Import the CSV file and create new mailbox-enabled users:

Import-CSV “C:Mailboxes.csv” | foreach {new-mailbox -Name $_.DisplayName –FirstName $_.Firstname –LastName $_.Lastname -DisplayName $_.DisplayName –Alias $_.alias  –Database $_.Database –password (ConvertTo-SecureString $_.password –AsPlainText –force) -ResetPasswordOnNextLogon $true -UserPrincipalName $_.UPN}

How it works

In this example we used the CSV to define the attributes we wanted to be enumerated when the mailbox-enabled user account was created.  To map the headers of the of the CSV file (row1 from the CSV image) to PowerShell variable, we us $_.. For example to define the first name for all mailbox-enabled user accounts we used the header FirstName in the CSV file. To capture the header in PowerShell we use $_.FirstName and input the value for parameter -FirstName.

The foreach command is used to step through the each row in a CSV file until a null value is returned. In our example, PowerShell will run the New-Mailbox command 5 times.

The RequestPasswordOnNextLogon in the example is set to $True. When the user logs on to a domain joined computer or Exchange Server 2013 Outlook Web App (OWA) the user will be prompted to change the password associated with the account.

Import-CAS can be used to create other recipient object. Using the Import-CSV with New-MailContact or New-DistributionGroup, you could create 1,000s of objects using one command. Using the Import-CSV can be used for almost any task with Exchange Server 2013 and is not limited to just creating recipient objects.

There’s more

Using the example, temporary passwords have been generated for 10 new mailbox-enabled user accounts. The Import-CSV cmdlet can be used to send out an email message notifying the new user’s manager of the temporary password for the newly created mailbox-enabled user account.

 

The CSV file contains the sender, recipients, subject of the email message and the body of the email message.

Next you can use the following code to generate email messages to the manager of the new user accounts, specifying the temporary password for each new user accounts:

Import-CSV “C:Message.csv” | foreach {Send-MailMessage -To $_.To -From $_.From -Subject $_.Subject -Body $_.Body -SmtpServer Server1}

The Send-MailMessage command, sends out emails message through the defined SmtpServer for each recipient defined in the CSV file.

Taking it a step further

When creating a new mailbox-enable users account you many want to add the users to a distribution group. Using the pervious example, lets create the mailbox-enable user and add it to a distribution group:

Import-CSV “C:Mailboxes.csv” | foreach {new-mailbox -Name $_.DisplayName –FirstName $_.Firstname –LastName $_.Lastname -DisplayName $_.DisplayName –Alias $_.alias  –Database $_.Database -Password (ConvertTo-SecureString -AsPlainText “Pa$$w0rd”) -Force -ResetPasswordOnNextLogon $true -UserPrincipalName $_.UPN

Add-DistributionGroup -Identity Sales -Member $_.DisplayName}

Build My Lab

Populating a development lab that reflected production can be time consuming and tedious. To create mail-enable users accounts in the development lab the mirrors production, you can use the Export-CSV on the production servers and then running Import-CSV on the development lab server.

From a production server run the Export-CSV cmdlet with the parameters that you want to bring over to the development lab.

Get-Mailbox | Select-Object Name, DisplayName, Alias, UserPr* | Export-Csv c:Mailbox.csv -NoTypeInformation

Select-Object is used instead of Format-List because when the value of each of the parameters is written to the CSV file. The NoTypeInformation parameter is used to prevent the header information to be written to the CSV file. .

Import-CSV “C:Mailbox.csv” | foreach {New-Mailbox -Name $_.Name -DisplayName $_.DisplayName -Alias $_.alias -Password (ConvertTo-SecureString -AsPlainText “Pa$$w0rd” -Force) -ResetPasswordOnNextLogon $true -UserPrincipalName $_.UserPrincipalName -Database DB1}

Once the CSV file containing the user information has been generated, the CSV file can be copied to a development lab Exchange Server 2013 server and the Import-CSV cmdlet can be used to import the user information.