Exchange 2010 export and delete scripts

Posted on 2012-08-20
Last Modified: 2012-08-20

I would like to export mail items from Exchange to pst format and then delete whatever I have exported excluding the calendar and task items.

I ran the following Script to export items:

New-MailboxExportRequest -Mailbox UserA -FilePath "\\SERVER\Share\UserA.pst"
 -ContentFilter {(Received -lt "12/31/2011")}

The export seem to have succeeded properly but when I run the following Delete command I get a date and time error:

I have tried various date formats of the following script
Search-Mailbox -Identity userA -SearchQuery 'received:<31/12/2011' -DeleteContent

I have slo tried:
Search-Mailbox -Identity userA -SearchQuery 'received:<31/12/2011 00:00:00' -DeleteContent

All of these Delete sctipts gave me a date and time format error (even though my server date is setup for dd/MM/yyyy as short date format)

I did however manage to delet ALL content from userA mailbox by means of changing the date as follow:

Search-Mailbox -Identity UserA -SearchQuery 'received:<2011/12/31' -DeleteContent

Please can someone assist be rectifying the Delete script so that only items older than 31/12/2011 gets deleted and calendar and taks are exluded from deletion.

Thank you
Question by:QGSmith
    LVL 49

    Expert Comment


    Search-Mailbox -Identity User -SearchQuery "Received:<$('12/21/2011')" -DeleteContent

    Author Comment

    Do you know if/how to exclude calendar and tasks in the script too?
    LVL 49

    Accepted Solution

    From a previous answer of mine

    Search-Mailbox -identity "mailboxname" -SearchQuery "kind:email AND Sent:> $('2006-12-31') AND Sent:< $('2008-01-01')"  -Confirm:$false -Force -TargetMailbox "targetuser" -TargetFolder "SearchAndDeleteLog" -LogLevel Full -DeleteContent

    Open in new window


    Author Comment

    Thank you for this. I will test and let you know if it works

    Author Comment

    Thank you Akhater

    Your Script worked by minipulating it this way:

    Search-Mailbox -identity UserA -SearchQuery "kind:email AND Sent:> $('2010-
    01-01') AND Sent:< $('2011-12-31')" -DeleteContent

    Thanks again

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Get an idea of what you should include in an email disclaimer with these Top 5 email disclaimer tips.
    Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
    The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now