- 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
- Learning Neo4j
- Android Jetpack開發:原理解析與應用實戰
- 企業級Java EE架構設計精深實踐
- 潮流:UI設計必修課
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- 跟“龍哥”學C語言編程
- Python高級編程
- Mastering macOS Programming
- MySQL數據庫基礎實例教程(微課版)
- Learning Python by Building Games
- JavaScript:Moving to ES2015
- INSTANT Silverlight 5 Animation
- JavaScript動態網頁編程
- Visual FoxPro 6.0程序設計
- Vue.js 3應用開發與核心源碼解析