PowerShell: List all the senders from an Outlook folder

A friend of mine was looking for a tool today to extract a list of names and email addresses from a folder in outlook. I know that Outlook has a comprehensive COM based object model which I figured that I should be able to access from Powershell. I quick search turned up articles from both James Manning and Lee Holmes on automating Outlook from Powershell (which I think I have come across before). A bit of poking around using the get-member helped me locate the properties I needed and resulted in the following script.

It access the Personal\Fun folder in my inbox and exports a list of names and email addresses for anyone that has sent me a joke (or at least those which were worth keeping)

$olFolderInbox = 6
$ol = new-object -comobject "Outlook.Application"
$mapi = $ol.getnamespace("mapi")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$msgs = $inbox.Folders.Item("Personal").Folders.Item("Fun")
$msgs.items | Select-Object SenderName, SenderEmailAddress -unique | export-Csv c:\emails.csv -noTypeInformation

Taking this a bit further, I wrapped this code into a script so that it could take in the path to an outlook folder and returned a collection of names and addresses.

So the following call will display the output to the console

.\get-OutlookFolderSenders.ps1 "Personal\Fun"

And to export them to a file you can just pipe through to the export-csv cmdlet

.\get-OutlookFolderSenders.ps1 "Personal\Fun" | export-csv "c:\email.csv" -noTypeInformation

Or if you want a html page you can do the following:

.\get-OutlookFolderSenders.ps1 "Personal\Fun" | convertTo-html | "c:\email.htm" 

 

I developed this little snippet of code using Powershell Analyzer which I still favour as my main Powershell IDE even though it is not longer being actively developed. It just fits with the way I like to work.

Print | posted on Wednesday, April 8, 2009 9:17 PM

Comments on this post

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
Power Shell is naively supported by Microsoft Management Studio as well. and i heard that the Visual Studio also going to be supported with PowerShell.
Left by VB Reader on Sep 24, 2009 11:57 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
I would like to add your blog to my blogroll please tell me what anchor should I use?
Left by Slot Machines virtuali on Oct 24, 2009 7:08 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
If you want the link to the home page, just use:
http://geekswithblogs.net/darrengosbell

If you want the RSS feed use:
http://feeds.feedburner.com/darrengosbell
Left by Darren Gosbell on Oct 25, 2009 7:49 AM

# Thank You

Requesting Gravatar...
Seriously, thank you so much for this. I just returned from a business trip and had ridiculous amounts of email in my Inbox for Outlook. This script helped me to know who to send emails to and tell them I'd get to them when I could.
Left by Nick Ohrn on Feb 23, 2010 5:03 AM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
Hi, maybe i am writing not in the right place but maybe you can help me...

Is it possible to create outlook email account with powershell? if Yes the how?
Left by RV on Dec 17, 2010 2:03 AM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
It probably is possible to add an account to outlook, but it's not something that I have attempted or know how to do. I had to do a bit of googling to get the code for this post working.
Left by Darren Gosbell on Dec 17, 2010 8:29 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
If you can help me with this i appreciate that. I still couldn't find the answer...
Left by RV on Dec 23, 2010 6:46 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
@RV - Sorry, this is really not something I have any idea how to do.
Left by Darren Gosbell on Dec 30, 2010 2:13 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
I'm having trouble with this. The script runs fine but refuses to accept that any folder locations I put in are correct.

So, say I run it with the "Personal" argument I get the following error :

An object could not be found

Any ideas?
Left by Stephen on Apr 13, 2015 9:55 PM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
@Stephen - hard to say what the issue is. What I would do is to open up PowerShell ISE, Paste in the sample script below this page and then highlight each line one at a time and hit F8. If it fails at a specific line this should give us an idea of where to look next. If you get all the way to the end it will list out all of the top level folders in your inbox so you will see what options you can pass to the main script. Maybe you don't have a folder called "Personal" ("personal" was a named folder I created under my inbox, so not everyone will have one).

$olFolderInbox = 6
$ol = new-object -comobject "Outlook.Application"
$mapi = $ol.getnamespace("mapi")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$inbox.Folders | select Name
Left by Darren Gosbell on Apr 14, 2015 7:27 AM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
Darren - FYI I think you've got a bad link to the script in OneDrive - it goes into a redirect loop.

https://onedrive.live.com/self.aspx/Public/Blog/PowerShell/get-OutlookFolderSenders.ps1?cid=98546e1b65a78a74&id=documents

Thanks for your contribution!
Left by Seth on Sep 05, 2015 5:05 AM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
Hmm, that's not me. I logged into OneDrive and I can't open the file from there either. All I get is "sorry something went wrong, we're working on it". I'll monitor it over the next few days and see if it resolves itself.
Left by Darren Gosbell on Sep 05, 2015 8:50 AM

# re: PowerShell: List all the senders from an Outlook folder

Requesting Gravatar...
How do i access multiple inbox folder in one script. Also, anyway to filter them using 'Filter option' to look for only particular message in subject line?
Left by SR on Feb 06, 2016 9:37 AM

Your comment:

 (will show your gravatar)