Link to home
Start Free TrialLog in
Avatar of jamorlando
jamorlandoFlag for United States of America

asked on

Script to Export Outlook E-mails to csv file

Hi,
We have a company Spam quarantine mailbox that uses message rules to organize the spam by employee into subfolders.

The goal is to have a script (powershell or vb script) that goes into each employee folder in the spam quarantine and e-mails them the From Field and Subject Field from all the emails in that folder from the previous day only.

Thanks for your help,
Jamie
Avatar of António Lima
António Lima
Flag of Portugal image


Hi Jamie,

I'm not sure if I understood what you need...
I think you are asking a way of extracting "From" and "Subject" from emails stored in a folder (?).

I presume you have MIcrosoft Exchange. Or would you need a script to run in all Outlook installed at your company?

Avatar of jamorlando

ASKER

Sorry, the setup is a bit confusing and I apologize if I explained it poorly.

All spam from the company is forwarded to a single inbox. spam@ourcompany.com
There is a Microsoft Outlook account on a PC that is configured for spam@ourcompany.com ... in addition, there are message rules set up that sort out each incoming spam into subfolders based on the employee.  For instance if spam is sent to Ricky@ourcompany.com, the email will go into the Ricky folder in the spam@ourcompany.com inbox.

I am looking to write a script that will email Ricky@ourcompany.com a daily report of spam that was received yesterday.  That way Ricky can browse through the email subjects and from fields to determine if any real e-mails got through that weren't spam.

Does this make sense?

Have a look at the redemption code for outlook scripting.

You can download it here:
www.dimastr.com/redemption/

Cheers
Daniel
Hmmm ... this software costs money.  There's gotta be a way to do this without using third party software.
Here's a bit about exchange cmd-lets for powershell that looks quite interesting.

http://technet.microsoft.com/en-us/library/bb266964%28EXCHG.80%29.aspx

Only thing is, it only applies to exchange 2007 and newer and is done directly on the exchange server, not through outlook.

Cheers
Daniel
It's preferable for me to do this from Exchange server actually... thanks for the link.  I'll take a look!
This is a great lead ... I can export a specific folder from a mailbox within a certain date range to a .PST file.  Now I just have to figure out a way to convert the .PST to a .CSV or .TXT file so that I can extract the information I need.
Yeah,

I hope you´re using exchange 2007 or 2010 then ;-)

Cheers
Daniel
We are using 2007.
Ugh ... and since we are running the 64 bit version this command doesn't work.  I need to set up the Exchange Management Tools on a 32 bit server.  That is ridiculously clunky to me.  Be back in a day or two..
http://exchangeinbox.com/article.aspx?i=102
Ok, I successfully exported a user's spam from yesterday into a .PST file and verified this by importing into an empty folder in Outlook.  Here's the command I used:

Export-Mailbox -Identity SPAM -IncludeFolders '\Inbox\Joe' -StartDate "10/13/2010" -EndDate "10/13/2010" -PSTFolderPath 'C:\Joe Yesterday Spam.pst'

Now ... what's the best way to convert this .pst file into a text file or csv file?
I just discovered that from within Outlook 2007 that you can do the following

File-->Import and Export...
Export To A File [Next]
Comma Seperate Values (Windows) [Next]
<Highlight Outlook Folder> [Next]
<Save Exported File> [Next]
<Map Custom Fields> [Finish]

Now how can I script that out with the command line or powershell?
Try this

Export-Mailbox -Identity SPAM -IncludeFolders '\Inbox\Joe' -StartDate "10/13/2010" -EndDate "10/13/2010"
| Export-CSV c:\EmailAddress.csv
@dax_bad:
Thank you, I tried that and received

If you are not exporting to a target mailbox folder or a .pst file, you must specify either parameter -DeleteContent or parameter -DeleteAssociatedMessages.
At line:1 char:1
+  <<<< Export-Mailbox -Identity SPAM -IncludeFolders '\Inbox\Joe' -StartDate "10/13/2010" -EndDate "10/13/2010" | Export-CSV c:\EmailAddress
.csv
    + CategoryInfo          : InvalidArgument: (:) [], RecipientTaskException
    + FullyQualifiedErrorId : 95FFF96F

When I tried this:
Export-Mailbox -Identity SPAM -IncludeFolders '\Inbox\Joe' -StartDate "10/13/2010" -EndDate "10/13/2010" -PSTFolderPath 'C:\Joe Yesterday Spam.pst' | Export-CSV c:\EmailAddress.csv

It actually executes successfully, however, it's not piping the output of the .pst file to the .csv file.  It's piping the logging data to the .csv file.

That was a good try though.  Any more thoughts?
Try to use the get-mailbox cmdlet instead with same parameters.
Running:
Get-Mailbox Joe | Export-CSV C:\export.csv

Is basically just putting the mailbox configuration data into a csv.
Basically the same output as
Get-Mailbox Joe | fl
ASKER CERTIFIED SOLUTION
Avatar of jamorlando
jamorlando
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial