Create contacts in mailboxes via Powershell?


  • Hi

    I have a problem, some of you may have a solution on.

    We have a customerdb in SQL which contains all our customers, all 16K! of them

    These customers have to be populated into a new folder, under Contacts, in approx 30 mailboxes. (Exch2013)

    I have managed to read the data from SQL and create the Contact-older in each mailbox, by using EWS.

    But they problem is that it takes approx. 1 hour(57min) to populate the ContactFolder per Mailbox. (and with only 24h per day, it cannot run each day in the state it is today)

    I was wondering if it is possible to solve this another way?

    I could dump the contacts into a csv-file, but I cannot find a script or an exchangecommand, that could import this csv other, than using EWS that I have already done.

    I dont have the possibility to have each user import the csv from their Outlook directly, it have to be centralized.

    Anyone that have a suggestion, or a link to an exchangecommand/powershellscript that may solve this?

    Thanks in advance,.


    • Edited by Peter_Moe Wednesday, June 13, 2018 1:42 PM
    Wednesday, June 13, 2018 1:42 PM

All replies

  • If you already have the contacts imported into one mailbox and its going to be the same folder in every mailbox you want to import then into. Probably the quickest method would be to use new-mailboxexportrequest and export just the folder from the Source Mailbox into a PST and the new-mailboximportrequest to import that into each of the target there an example of the syntax needed in

    Your EWS code is probably slow because your just creating one contact at a time, EWS has batching operations so you can create multiple contacts in one call (with contacts probably between 20-50 in a batch is reasonable depending on the number of properties you setting). Probably still won't be as quick as new-mailboximportrequest but should be a lot quicker.


    Wednesday, June 13, 2018 11:31 PM
  • Thanks Glen

    How do I usebatch-operations with EWS?

    My code is nearly identical to this:

    I have looked here:

    But this code is in C, and I cannot make heads and tails on how to port this to powershell

    Anyway you may help me out here or point me in another direction?

    I'll look into the mailboxexportrequest-method aswell

    Thanks in advance.


    • Edited by Peter_Moe Thursday, June 14, 2018 9:12 AM
    Thursday, June 14, 2018 9:02 AM
  • Where you have $ at the moment you will need to rewrite this and work that logic so you build a batch of items using a Generic list and then use the CreateItems method of the ExchangeService object

    Because your submitting the creates in batches you should then write code to process the results and handling throttling if necessary as you may find that the batch got half way and the server throttled the response etc so then some of the items in the batch need to be retried. If your going to be doing this on a regular basis then it worth investing the time to make it robust.


    Friday, June 15, 2018 2:48 AM
  • Thanks alot Glen.

    One more(hopefully the last),

    I'm trying to batch just 10 contacts to try out this CreateItems function, but I get an odd error:

    $ContactsFolder = [Microsoft.Exchange.WebServices.Data.ContactsFolder]::Bind($service,$ContactsFolderSearch.Id);
    $myarray = New-Object 'System.Collections.Generic.List[Microsoft.Exchange.WebServices.Data.Item]'
    foreach ($t in $p0) {
     $service.CreateItems($myarray, $ContactsFolder.Id, $null, $null);
    Exception calling "CreateItems" with 4 arguments: "This operation can't be performed because at least one item already has an ID."

    I have search on Google, but havent found anything regarding this error, do you have a clue what this mean?

    Thanks in advance,


    Friday, June 15, 2018 12:57 PM