Tuesday, March 11, 2014

Getting list of Outlook contacts using PowerShell Script.

Quote of the Day “We can't help everyone, but everyone can help someone. - Ronald Reagan

Greeting of the day.

Today I was working on a script, that script need to access the contact folder of the users default Outlook profile.

Managing Outlook and make it working using PowerShell is very easy, the only complicated part is to make the Outlook items working with it. You have to know, which code to use to access “Contacts”, and “Calendars” items or so on.

I have written a small script, which can retrieve a list of contacts from your default outlook profile. You can download the script from the below TechNet script gallery.

Once you have contact data in your hand, you can export it to CSV, HTML, or so, and I think, it is quick and easy way to back up your contacts {if you or your users still use POP3/IMAP with Outlook}.
Why I need this kind of script to retrieve contacts from the outlook, you may know the answer in my coming blog posts.


11-03-2014 13-16-23

The script is fairly simple and the only complicated part of the script is :
$contactObject  = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)

Rest of all is easy to understand. In this script,
1.    First we are adding the assemply for Microsoft Outlook.
2.    Then we are creating  com object of “Microsoft Application”
3.     Then we are getting the default MAPI namespace for outlook.
4.    Then we are binding contact folder in to “$contactObject
5.    After that we are using Foreach to iterate each entry in the “$contactObject
6.    You are saving the result in the in a custom object.

Script :

#==================| Satnaam Waheguru Ji |===============================
#            Author  :  Aman Dhally 
#            E-Mail  :  amandhally@gmail.com 
#            website :  www.amandhally.net 
#            twitter :   @AmanDhally 
#            blog    : http://newdelhipowershellusergroup.blogspot.in/
#            facebook: http://www.facebook.com/groups/254997707860848/ 
#            Linkedin: http://www.linkedin.com/profile/view?id=23651495 
#            Creation Date    : 11-03-2014
#            File    :          Playing with KeyBaord 
#            Purpose :  Accessing Outlook Contacts using PowerShell       
#            Version : 1 
#            My Pet Spider :          /^(o.o)^\  

              # Try one or more commands
              try {
                     Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
                     $Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
                     $namespace = $Outlook.GetNameSpace("MAPI")
                     $contactObject  = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
                     $contactList = $contactObject.Items;
              # Catch all other exceptions thrown by one of those commands
              catch {
              # Execute these commands even if there is an exception thrown from the try block
              finally {


              foreach ( $name in $contactList ) {
                     $props = @{
                           #'Title' = $name.Title;
                           'FullName' = $name.FullName;
                           'FirstName' = $name.FirstName;
                           'Email Address' = $name.Email1Address;
                           'Mobile' = $name.MobileTelephoneNumber;
               $object = New-Object -TypeName PsObject -Property $props
                     Write-Output $object
                     # You may find more contact field details from this link => : http://msdn.microsoft.com/en-us/library/ee160254(v=exchg.80).aspx




# end of the script.

I hope you find it useful.
Aman Dhally
clip_image017 clip_image018 clip_image019 clip_image020 imageclip_image021

1 comment:

  1. Nice post, first time i was recover my accidentally deleted contact list though the help of PowerShell Script. Before this i can use third party repair tool for the restore outlook contacts because i regularly lost my outlook contacts list.


Note: Only a member of this blog may post a comment.