A Powershell Script To Copy The Contacts From An Exchange Public Folder To All Mailbox Contacts Folders

[ 14 ] Comments

This is a script to copy Contacts from an Exchange Public Folder into the Contact folders of all Mailboxes.  It uses the script for an individual mailbox that I wrote here http://blog.leederbyshire.com/2015/05/06/a-powershell-script-to-copy-the-contacts-from-an-exchange-public-folder-to-a-mailbox-contacts-folder/ (which you should create in the same folder as the script you will create for this article), and wraps it in an enclosing loop for all mailboxes.

It can be run in either Exchange Management Shell, or the plain PowerShell (since it loads the Exchange Admin snap-ins by itself).  Because it needs the snap-ins, it should be run on the Exchange Server itself, or maybe it will work if only the Management Console is installed – I haven’t tried.  Note that the account you are logged in with will need application impersonation permission on all the mailboxes.

If you want to try it, copy the code into  a .ps1 file (e.g. CopyPFToContactsAllMailboxes.ps1) and run it from the shell by navigating to the folder you put it (and the script from the page linked above) in, and typing

.\CopyPFToContactsAllMailboxes.ps1 <folderPath> <clearFolder>

where folderPath is the path of the Public Folder to Copy, and clearFolder is a flag to indicate whether to empty the mailbox folders first.  Here is an example:

.\CopyPFToContactsAllMailboxes.ps1 \PublicContacts\WorldWide $True

If you want to copy the contacts to a folder that is not the default Contacts folder, you can supply the destination folder as the third positional parameter, like this

.\CopyPFToContactsAllMailboxes.ps1 \Folder\SubFolder $True "\Contacts\Sub Folder"

or as a named parameter (if you are omitting the second parameter), like this

.\CopyPFToContactsAllMailboxes.ps1 \Folder\SubFolder destination "\Contacts\Sub Folder"

In either case, the destination folder is specified as a path relative to the mailbox root, not the default Contacts folder. If you want the script to create the folder, you can supply the parameter -createFolder $True. Remember that if you are supplying all parameters to the script, and in the correct order, you can omit the parameter names.

If your Public Folder contents are subject to frequent change, I’d suggest running it in a scheduled task when things are quiet.

# CopyPFToContactsAllMailboxes.ps1
# By Lee Derbyshire
# Parameter 0 = Public Folder Path
# Parameter 1 = Empty folders first?
# Parameter 2 = Destination folder if not default Contacts folder
# Parameter 3 = Create destination folder if absent?
# Usage examples:
# CopyPFToContactsAllMailboxes.ps1 \Folder\SubFolder $False
# CopyPFToContactsAllMailboxes.ps1 \Folder\SubFolder $True "\Contacts\Sub Folder"
# CopyPFToContactsAllMailboxes.ps1 \Folder\SubFolder -destination "\Contacts\Sub Folder"

param([Parameter(Position=0,Mandatory=$True,HelpMessage="The path of the folder to copy")]
      [Parameter(Position=1,Mandatory=$False,HelpMessage="Whether or not to empty the folder first")]
      [Parameter(Position=2,Mandatory=$False,HelpMessage="The path of the destination folder")]
      [Parameter(Position=3,Mandatory=$False,HelpMessage="Whether or not to create the destination folder")]

# Add the snapin in case we're in the plain (i.e. non-Exchange Management) Shell

# Try the E2007 snapin first
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue
# Then try the E2010 snapin
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue

Get-Mailbox | foreach {
"Processing mailbox $_"
.\CopyPFToContacts.ps1 -folderPath $folderPath -mailbox $_.PrimarySMTPAddress -clearFolder $clearFolder -destination $destination -createFolder $createFolder

14 Responses to A Powershell Script To Copy The Contacts From An Exchange Public Folder To All Mailbox Contacts Folders

  1. Martin Lohrer says:

    Hello an thank you very much for this wonderful solution!

    Could you please give me an hint, how this is done to copy the the contacts to an subfolder of each personal mailbox? I’m not sure, where to add the -destination parameter.


    • admin says:

      Hello Martin. I forgot to update this article after I’d updated the other one since adding the destination parameter. Anyway, I just updated it, and it should make more sense. I hope.

  2. Kevin Pare says:

    any tips on how to run it as a scheduled task?

    • admin says:

      Sure, do you know how to use the task scheduler at all, or are you just wondering what to put in as the command?

  3. Dan Powell says:

    will this work with office 365

    • admin says:

      Hi Dan – I honestly have no idea. I’ve read plenty of things that say that EWS works in the same way as on-premises, but having the mailbox off-site could certainly create issues that aren’t directly related to Exchange itself. Unfortunately, I don’t have an O365 mailbox to try it with, so I can only suggest that you try it out, and see what happens.

  4. Yuksel BASTAN says:

    hi, i have a question and i hope you can help me

    i have a customer list with more than 1000 customers,
    some of them have 1 external mail adres some have more then 2,
    now how can i import this list

    Name FistName LastName ExternalEmailAdress1 ExternalEmailAdress2 ExternalEmailAdress3
    Name1 FistName1 LastName1 mail1@mail1.com mail2@mail2.com mail3@mail3.com
    Name2 FistName2 LastName2 mail1@mail1.com mail2@mail2.com mail3@mail3.com
    Name3 FistName3 LastName3 mail1@mail1.com mail2@mail2.com mail3@mail3.com
    Name4 FistName4 LastName4 mail1@mail1.com mail2@mail2.com mail3@mail3.com
    Name5 FistName5 LastName5 mail1@mail1.com mail2@mail2.com mail3@mail3.com

  5. Yüksel BASTAN says:

    i think Steve Goodman is a very busy man he is not answering. Now im in a bad position :S

    • admin says:

      Hello again. There is no need to contact Steve directly – the script he describes can be downloaded from the page that I sent you a link to. It is not easy to see, but it is there.

  6. Yüksel BASTAN says:

    ok i will look again. Thank you very much for your help and answeres.

  7. Sheed says:

    Forgive me I’m a little new to the Exchange world.
    How can I find the exact path of my public folders where we have the shared folder for contacts?

Leave a Reply to Yüksel BASTAN Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>