×

Uploading Bulk Contacts on Global Address Book Using CSV File in Microsoft 365

Microsoft 365 Global Address List is a contact repository that contains the contacts that are to be shared with all the members within an organization. These contacts may be from internal organization or outside or partner the organization.

Uploading Bulk Contacts on Global Address Book Using CSV File in Microsoft 365

Microsoft 365 portal doesn't offer you an easy way to import multiple contacts into your company's Global Address List (GAL), unlike the Contacts app as service. To bulk import contacts, one of the efficient ways is to use PowerShell. Here are the instructions on how to use PowerShell for importing multiple contacts using a .csv file.

Gathering Contacts

1. Create a contact CSV (comma-separated value) file containing the following headers.

ExternalEmailAddress

Name 

FirstName

LastName 

StreetAddress

City  

StateorProvince

PostalCode 

Phone 

MobilePhone

*Type in precisely as mentioned above.

2. Please note that you must provide an email address [ExternalEmailAddress] for each contact. If any of the employees do not have an email address, exclude those records from this list because those cannot be processed and imported.
You must fill out these two mandatory columns for each contact.

  • ExternalEmailAddress
  • Name

*The other columns are optional. If any column is left blank, it will not be imported into the GAL.

Code execution and its explanation:

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned (Setting Up Execution Policy and set it RemoteSigned).

Execution Policy Change

The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at

http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

(Set it to Yes or Yes to All )

PS C:\Windows\system32> $UserCredential=Get-Credential (Provide the server administrator credentials)

PS C:\Windows\system32> $s=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -credential $UserCredential -Authentication Basic -AllowRedirection

(Getting the session generated from the exchange server for remote connectivity and storing it in the variable s)

PS C:\Windows\system32> import-PSSession -session $s

(Importing the session so that all the commands like New-MailContact, New-MailBox, etc. controls can operate on cmd script)

 

We are creating the system pop-up dialog box for importing the CSV file. Please note ‘param’ might show warning messages, but you can ignore it.

param(

    [Parameter(ValueFromPipeline=$true,HelpMessage="Enter CSV path(s)")]

    [String[]]$Path = $null

)

if($Path -eq $null) {

    Add-Type -AssemblyName System.Windows.Forms

    $Dialog = New-Object System.Windows.Forms.OpenFileDialog

    $Dialog.InitialDirectory = "$InitialDirectory"

    $Dialog.Title = "Select CSV File(s)"

    $Dialog.Filter = "CSV File(s)|*.csv"        

    $Dialog.Multiselect=$true

    $Result = $Dialog.ShowDialog()

    if($Result -eq 'OK') {

        Try {

            $Path = $Dialog.FileNames

        }

        Catch {

            $Path = $null

            Break

        }

    }

    else {

        #Shows upon cancellation of Save Menu

        Write-Host -ForegroundColor Yellow "Notice: No file(s) selected."

        Break

    }

    $Date = Get-Date

Write-Host "You input:'$Path' on '$Date'"

}

 

 

Display the CSV file imported.

Import-Csv $Path

Uploading the imported CSV file.

Import-Csv $Path |%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}

 

Please note the following:

1. Marked in red (Error message) may come if the contacts are already added in the GA, or there is some error in the CSV file.

2. In our case, we have added only ExternalEmailAddress and Name. In order to customize more inputs, please modify the command as follows.

Import-Csv $Path |%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName -StreetAddress $_.StreetAddress} and so on.

Syntax: 

-ExternalEmailAddress $_.(CSV File Coloumn Name)

-Name $_.(CSV File Coloumn Name)

-FirstName $_.(CSV File Coloumn Name)

-LastName $_.(CSV File Coloumn Name)

-StreetAddress $_.(CSV File Coloumn Name)

-City  $_.(CSV File Coloumn Name)

-StateorProvince $_.(CSV File Coloumn Name)

-PostalCode $_.(CSV File Coloumn Name)

-Phone $_.(CSV File Coloumn Name)

-MobilePhone $_.(CSV File Coloumn Name)

3. Other parameters which can be used with New-MailContact cmdlet are as follows:

New-MailContact

   -Name <String>

   -ExternalEmailAddress <ProxyAddress>

   [-Alias <String>]

   [-ArbitrationMailbox <MailboxIdParameter>]

   [-Confirm]

   [-DisplayName <String>]

   [-DomainController <Fqdn>]

   [-FirstName <String>]

   [-Initials <String>]

   [-LastName <String>]

   [-MacAttachmentFormat <MacAttachmentFormat>]

   [-MessageBodyFormat <MessageBodyFormat>]

   [-MessageFormat <MessageFormat>]

   [-ModeratedBy <MultiValuedProperty>]

   [-ModerationEnabled <Boolean>]

   [-OrganizationalUnit <OrganizationalUnitIdParameter>]

   [-OverrideRecipientQuotas]

   [-PrimarySmtpAddress <SmtpAddress>]

   [-SendModerationNotifications <TransportModerationNotificationFlags>]

   [-UsePreferMessageFormat <Boolean>]

   [-WhatIf]

   [<CommonParameters>]

For more information on  parameters, please refer to the link:

https://docs.microsoft.com/en-us/powershell/module/exchange/new-mailcontact?view=exchange-ps

Contacts are now available on the admin console.

You can also copy and paste the complete code on windows PowerShell ISE and save it as .ps1 file and run the same. 

Set-ExecutionPolicy RemoteSigned 

$UserCredential=Get-Credential

$s=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -credential $UserCredential -Authentication Basic -AllowRedirection

import-PSSession -session $s

param(

    [Parameter(ValueFromPipeline=$true,HelpMessage="Enter CSV path(s)")]

    [String[]]$Path = $null

)

if($Path -eq $null) {

    Add-Type -AssemblyName System.Windows.Forms

    $Dialog = New-Object System.Windows.Forms.OpenFileDialog

    $Dialog.InitialDirectory = "$InitialDirectory"

    $Dialog.Title = "Select CSV File(s)"

    $Dialog.Filter = "CSV File(s)|*.csv"        

    $Dialog.Multiselect=$true

    $Result = $Dialog.ShowDialog()

    if($Result -eq 'OK') {

        Try {

            $Path = $Dialog.FileNames

        }

        Catch {

            $Path = $null

            Break

        }

    }

    else {

        #Shows upon cancellation of Save Menu

        Write-Host -ForegroundColor Yellow "Notice: No file(s) selected."

        Break

    }

    $Date = Get-Date

Write-Host "You input :'$Path' on '$Date'"

}

Write-Host "Displaying Content of the .CSV File. Please remember (ExternalEmailAddress & Name are two default fields to add contacts to exchange and has to be the first two-column). ExternalEmailAddress

Name 

FirstName

LastName 

StreetAddress

City  

StateorProvince

PostalCode 

Phone 

MobilePhone "

Import-Csv $Path

Import-Csv $Path |%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}




Trendy