Showing posts with label Outlook Automation using Powershell. Show all posts
Showing posts with label Outlook Automation using Powershell. Show all posts

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.



Screenshot

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)^\  
#========================================================================


BEGIN
       {
      
              # 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 {
             
                     $OutlookError
                     $ApplicationError
              }
              # Execute these commands even if there is an exception thrown from the try block
              finally {
              }
      
      
       }

PROCESS
       {
      

              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
       {
      

       }

# end of the script.


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

Friday, August 30, 2013

Powershell and Outlook : Create and send a new email using Powershell “OutlookTools” Module.

 

Hi,

I have added a new functionality in “OutlookTools” module. Now we can compose and send a new emails using “New-OutlookEmail” function.

This function also do support adding attachments to the composed email, but for now we can attach only one single file.

“OutlookTools” Module download link: https://github.com/AmanDhally/OutlookTools

Let’s start

 

  1. First download the “OutlooTools.Psm1” and .psd1 file fromhttps://github.com/AmanDhally/OutlookTools
  2. Create a folder Name “OutlookTools” in “Libraries\Documents\WindowsPowershell\Modules
  3. and paste both files there.

12-08-2013 1 
Now open Powershell console and run the cmdlet, 

Import-Module OutlookTools

12-08-2013 2

By running the Get-Command cmdlet, we can see which cmdlet and function this modules had added.

Get-Command -Module outlooktools

0

You can see now , we have New-OutlookEmail listed in Function.

How to use it?

It’s simple.

Create a new email.

We are creating a new email , to amandhally@gmail.com with subject “Sending from Powershell” and with body text “Heya its working”

New-OutlookEmail -To amandhally@gmail.com -Subject "Sending from Powershell" -Body "Heya! its working"

1

and when you hit enter, you encountered this problem. ,

I haven’t solved it yet and i am working on it. :( .

2 

but when you click on “Allow”. this will send an email.

You can see, the email is come to our Sent Items folder.

3

and i received it on my Gmail id too.

4

Now create an email with attachment.

Now , we are creating a new email , with attachment, and with High Priority.

New-OutlookEmail -To "amandhally@gmail.com" -Subject "Attaching file"  -Attachment 'D:\a.txt' -importance 2

5

now when you click on send, the above problem comes again with one more confirmation box.,

6

7

once you click on Allows on both.

the email will be in Sent Item

8

and i received it on my Gmail id with attachments too.

9

“OutlookTools” Module download link: https://github.com/AmanDhally/OutlookTools

i hope you will like it.

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007