- 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
- Oracle從入門到精通(第3版)
- 軟件安全技術
- Android和PHP開發最佳實踐(第2版)
- 深入理解Django:框架內幕與實現原理
- Learning Linux Binary Analysis
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- JavaScript:Moving to ES2015
- H5頁面設計:Mugeda版(微課版)
- C#開發案例精粹
- 區塊鏈國產化實踐指南:基于Fabric 2.0
- SQL Server 入門很輕松(微課超值版)
- Access數據庫應用教程(2010版)
- JavaScript前端開發基礎教程
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- Spring Boot從入門到實戰