Export Active Directory (AD) user accounts with specific email address and import as contacts to Office 365

I have a two Office 365 tenant accounts, with one on-premise Exchange organization. Currently two companies share the same on-premise Exchange organization. As part of moving mailboxes to the cloud, we are separating the companies by moving them to their own Office 365 tenant account. This brings us numerous problems and different functionality, with one major limitation is a split GAL. PowerShell can assist us.

  • I need to get a list of all possible properties for the Get-Mailbox cmdlet.
    Get-Mailbox -identity user@domain.com.au | fl

    This will display all the possible fields that we can use to do a custom search.

  • I want to use the PrimarySmtpAddress field and do a custom search for email addresses like *@domain.com
    Get-Mailbox | Where-Object {$_.PrimarySmtpAddress –like "*@domain.com"}

    See Using the Where-Object Cmdlet for more information on the Where-Object Cmdlet.

    This command above will display all users with a PrimarySmtpAddress with @domain.com. However this isn’t enough information, I need contact information, e.g. address and phone numbers. The Get-Mailbox cmdlet is the wrong command to use in this instance.

  • I will need to export the information from the actual user account itself, so I can include the mobile and telephoneNumber properties along with other properties.Run the following in company A’s on-premise Active Directory using using a domain controller and the Active Directory PowerShell module.
    Import-Module activedirectory 

    Get-ADUser –identity <alias> –Properties *

    This will display all possible properties for my user account so we know what we need to use. However, I only want users with a @domain.com EmailAddress. I also want specific properties exported to a CSV file.

  • I will need to run the following command to export a list of users from Active Directory with an email address that ends with @domain.com
    Get-ADUser -Filter 'EmailAddress -like "*@domain.com"' -Properties * | Select-Object -Property Name,DisplayName,Title,EmailAddress,GivenName,sn,Initials,StreetAddress,Office,City,State,PostalCode,Country,OfficePhone,Company,HomePhone,mobile,Department | Sort-Object -Property Name | export-csv .\UserPropertiesCSV.csv

    We are using 3 cmdlet’s here in this blog post, Get-ADUser, New-MailContact & Set-Contact. The table below shows the similarities between the properties for each cmdlet.




City City
Company Company
Country CountryOrRegion
Department Department
DisplayName DisplayName
EmailAddress ExternalEmailAddress
GivenName FirstName
HomePhone HomePhone
Initials Initials
Mobile MobilePhone
Name Name Name
Office Office
OfficePhone Phone
PostalCode PostalCode
sn LastName
State StateorProvince
StreetAddress StreetAddress
Title Title
  1. Using company B’s tenant account, connect to remote PowerShell on a separate server to where you extracted company A’s user data.
    Import-module msonline 
    Connect-MsolService –Credential $LiveCred 
     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ –Credential $LiveCred -Authentication Basic –AllowRedirection 

    Import-PSSession $Session (If you get an error with this cmdlet, run this Set-ExecutionPolicy Unrestricted)

  2. Import contacts using the company A information from the CSV file using the New-MailContact cmdlet:
    Import-Csv .\UserPropertiesCSV.csv|%{New-MailContact -Name $_.Name -DisplayName $_.DisplayName -ExternalEmailAddress $_.EmailAddress -FirstName $_.GivenName -LastName $_.sn} 

    Set the extended properties for each contact in the CSV by using the Set-Contact cmdlet:

    $Contacts = Import-CSV .\UserPropertiesCSV.csv
    $contacts | ForEach {Set-Contact $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateorProvince $_.State -PostalCode $_.PostalCode –CountryOrRegion $_.Country -Phone $_.OfficePhone -MobilePhone $_.Mobile -HomePhone $_.HomePhone -Company $_.Company -Title $_.Title -Department $_.Department -Initials $_.Initials -Office $_.Office}

One Comment

  1. Hello There. I found your blog the usage of msn. That is a very smartly written article.
    I will be sure to bookmark it and come back to learn extra of
    your helpful information. Thank you for the post.
    I’ll definitely return.

Leave a Reply to storify.com Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s