How to export sent items for date range for all mailboxes - I actually only need the sent to addresses, FYI.

Dave Heritage 1 Reputation point

The actual need: Get all email addresses sent 'to' from all mailboxes to act as a primer for an upcoming email marketing campaign. There is no existing customer management system that contains the data set desired.

I have seen a read a number of articles, some of which have a lot of code for EWS but no explanation of where to get that nor how to set it up. That said, My thought process was to export all mailbox's sent items into a large mailbox, (then export that to PST if required?), and try to use outlook to export the addresses that were sent 'to'. That said, the step where I go to pull the sent items in powershell is not working as expected.

If I run
search-mailbox usermailbox -searchquery 'sent:1/1/2020..5/20/2021 AND from:user@keyman .com' -LogLevel Full -Targetmailbox "holdingBox" -Targetfolder "usermailbox"

then I get data. I cannot speak to how 'good' it may be, but let's say it is 1k items.

BUT if I run (because I ultimately will be grabbing them all)

get-mailbox usermailbox | foreach-object {$faddr = $.PrimarySMTPAddress.address.tostring();search-mailbox $.identity -searchquery 'sent:1/1/2020..5/20/2021 AND from:$faddr' -LogLevel Full -Targetmailbox "holdingBox" -Targetfolder $_.Name}

then I get nothing. Not sure what my issue may be with parsing etc, but obviously I have something wrong.

In the above, and running commands manually, $faddr does in fact contain user@keyman .com as a string. I was thinking it would pass properly, but ti doesn't seem to do so.

Perhaps this is totally the wrong way to go about, and feel free to correct any silliness.

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,208 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Eric Yin-MSFT 4,386 Reputation points

    The command "get-mailbox usermailbox | foreach-object {$faddr = $.PrimarySMTPAddress.address.tostring();search-mailbox $.identity -searchquery 'sent:1/1/2020..5/20/2021 AND from:$faddr' -LogLevel Full -Targetmailbox "holdingBox" -Targetfolder $_.Name}", are you trying to run search-mailbox for ALL mailboxes?

    If so, you should remove "usermailbox" so that Get-mailbox could return all mailboxes, isn't it?
    Or if you only want user mailboxes, use "Get-Mailbox -ResultSize Unlimited | where {$_.RecipientTypeDetails -eq "UserMailbox"}" instead.

    I can't say the command would work after you modify it because I got another error about that, and we are not powershell/scripting forum here, if you want more help with scripting, tell me so that I would modify the tag for you.

    Another method I could think of is searching the message tracking log, but it only saved 30 days by default. Check if you changed it with "get-transportservice |fl MessageTrackingLogMaxAge".
    If the MessageTrackingLogMaxAge is in your date range, then you can export the Sender and Recipients to a csv:

    get-messagetrackinglog -resultsize unlimited -EventID "SEND" -Start "2014/08/01 00:01:00 AM" -End "2015/02/10 23:59:00 PM" | select-object Sender,@{Name=”Recipients”;Expression={$_.recipients}}| export-Csv C:\Export.csv  

    Last method, it may not help because I don't know how to do it bulkly, just FYI.
    Login one user's Outlook, go to File-Export-Import/Export-Export to a file- Comma seperated values, find the Sent Items and save it to a csv.
    Then you can open the csv with Excel and select the "To: (Name)" or some other values you want.

    In general, I still think the best method is to get some help from powershell experts, what do you think?

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.