- Microsoft Exchange Server PowerShell Essentials
- Biswanath Banerjee
- 573字
- 2021-07-16 13:04:59
Import export of objects
As an Exchange administrator, you will be asked to create new users, contacts, and export content from Active Directory. This section will show you how to import and export objects to and from Active Directory.
Import user accounts in bulk using a CSV file
If you are an administrator tasked with adding lots of mailboxes for new users who have joined the company, you can use PowerShell to create these users in bulk.
First, you need a CSV file with the list of users that has to be created. The format of the CSV file will look like the following screenshot:

Save this file as UserList.csv
.
The first line of the CSV file is important as it will indicate the corresponding values for the fields such as FirstName
, Alias
, and more for these users. Another thing to notice here is the presence of OU as I wanted to create these users in separate OUs in Active Directory. The password here is the same for simplicity, but it doesn't have to be this way; you can choose to use different passwords for different users.
The next step is to create a few variables to store the data that we will use in the script later. We will use the $Database
, $Password
, and $Userlist
variables. The $Password
variable will store the password and will use the CovertTo-SecureString
function to convert plain text passwords into a secure string.
You need to check with your Exchange administrator to get the value of the $Database
variable as all the mailboxes will be provisioned in this database, or you can choose the automatic mailbox distribution feature in Exchange 2013 that was explained earlier in this chapter. I am using the default database in Exchange 2013; and hence, you will see a random number at the end. You won't see this if you have a naming convention for your databases in your Exchange Organization.
The script will look as this. Save this script as CreateMailboxes.ps1
:
$Database = "Mailbox Database 2020596250" $Userlist = Import-CSV Userlist.csv ForEach ($User in $Userlist) { $Password = ConvertTo-SecureString $User.Password -AsPlainText -Force New-mailbox -Password $Password -Database $Database -UserprincipalName $User.UPN -Alias $User.alias -Name $User.Displayname -FirstName $User.Firstname -LastName $User.LastName -OrganizationalUnit $User.OU }
Import external contacts in bulk using a CSV file
Another frequent request that Exchange and Active Directory administrators get is to import a list of contacts with the external email address from a file. In this section, we will talk about the process. Like we did while importing users, the first step is to create a CSV file with the following columns:

Save this file as ExternalContacts.csv
. The next step is to create an Organizational Unit in Active Directory where these contacts will be imported. If you already have one created, skip this step.
The following command will take the input from the previous CSV file and create the contacts in the Contacts OU under Contoso.com
:
Import-Csv Externalcontacts.csv | ForEach {New-MailContact -Name $_.DisplayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit contoso.com/contacts}
Exporting objects to a CSV file
At times, as an Active Directory or Exchange Administrator, you will be asked to export user objects to a CSV file. Here is a simple way to achieve this with a combination of Get-ADUser
, Select-Object
, and Export-CSV
cmdlets. You can modify the properties that are selected based on your needs:
Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,CompanywhenCreated,Enabled,MemberOf | Sort-Object -Property Name | export-csv c:\temp\ADUsers.csv
- JBoss Weld CDI for Java Platform
- Implementing VMware Horizon 7(Second Edition)
- Rust實戰
- Magento 2 Theme Design(Second Edition)
- C#程序設計(慕課版)
- Python爬蟲開發與項目實戰
- Learning AWS Lumberyard Game Development
- Java程序員面試算法寶典
- 機器人Python青少年編程開發實例
- PHP 編程從入門到實踐
- C語言實驗指導及習題解析
- Learning SciPy for Numerical and Scientific Computing(Second Edition)
- ASP.NET求職寶典
- ASP.NET 4.0 Web程序設計
- Mastering Android Studio 3