Link to home
Start Free TrialLog in
Avatar of RichardPWolf
RichardPWolfFlag for United States of America

asked on

Anybody use the New-Mailbox cmdlet

I have a script to clean out mailbox(s) that currently use the search-mailbox cmdlet and it works for most cases. But I have one mailbox (mail journal) that houses a copy of all emails coming and going and once a month after backing it up I delete the content that's older than 1 month. Search-Mailbox works for the fist 10000 but progressively returns less and less items even though there are usually still about 20000 + emails that need to be deleted. I've been looking at the New-MailboxSearch cmdlet but I don't see any way to delete content. Anybody overcome this issue?

Thanks Richard
Avatar of Jian An Lim
Jian An Lim
Flag of Australia image

Avatar of RichardPWolf

ASKER

As I mentioned that's currently what I use but..... let's say I have 30000 messages to delete. First run will delete 10000, second run maybe 5000, third run maybe 10 and after that 0. But when I go into the mailbox there's still 14990 messages that need to be deleted. This happened sporadically when we were on Exchange 2010 but happens all the time now that we're on 2013.
my understanding, there are no other powershell to delete content except this command.

Unless, you want to login as this user and use outlook OR use archiving method to move them out from the mailbox to archived mailbox then delete.

https://technet.microsoft.com/en-us/library/aa996035%28v=exchg.141%29.aspx?f=255&MSPPError=-2147217396
Could you run this using Outlook?
I used this method back when I had exchange 2003 and all my journaling was done on my system. We have since moved to a hosted spam filter and archiving so this task has become a thing of past.
ASKER CERTIFIED SOLUTION
Avatar of dan_blagut
dan_blagut
Flag of France 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
limjianan, yo_bee - Currently that's how I'm doing it but the whole idea is to automate it as I have enough on my plate without logging in as the mail journaling to use outlook on another system. This take a lot of time away from other duties I have.
dan_blagut - His code is very similar to mine. Here's mine;
#*=============================================
 #* Remove Previous Months emails from MJournal
 #*=============================================
 Write-Host "Removing Old Entries from MJournal" -foreground "green"
 Write-Verbose "If all good to this point delete old email using $edate"
      Get-Mailbox -Identity "Mail Journal" | Search-Mailbox -SearchQuery {(Received -le '$edate')} -DeleteContent -force
yes, i agreed. as a result, my recommendation is to use managed folder mailbox policy.

In theory, the script should run well. there are some thing we don't have a clear picture of that causing the email.


understand the managed folder mailbox policy
https://technet.microsoft.com/en-us/library/ee364744%28v=exchg.141%29.aspx

other instruction
https://technet.microsoft.com/en-us/library/aa996035%28v=exchg.141%29.aspx
limjianan - I don't see where managed mailboxes are relevant to my question. This isn't about how the users manage there emails but maintenance on a single mailbox that holds a copy of all emails and at the end of the month gets backed up and stored. Just to clarify I'm anal about backups and hold at least two types to insure that I can comply with e-discovery and other regulatory requirements.
you can use managed mailbox and apply to this particular mailbox, then use this tool to remove the emails accordingly.
This is just one of a creative way to solve an issue that normally a command should run.

if not, there will be alternative way is to create a new mailbox because I suggest the mailbox might in issue.
Make a routine to change to a new journal mailbox every month to avoid these kind of issues.

Fundamentally, there are something missing in the pictures that I am just putting our 2 cents  :)
I see now where you're going with the managed mailbox but I don't understand how that is going to mitigate the problem with deleting more than 10000 emails at a time or that it seems that it doesn't see the total remaining emails when the script runs. Could it be a sync issue with the mailbox and the exchange in that it takes time for the exchange to actually delete the emails and get new totals?
It could be anything as i originally said.
it could be your health of your mailbox database.
it could be indexing issue.
it could be the system don't like -SearchQuery {(Received -le '$edate')} ONLY, you might need to use -SearchQuery {(Received -le '$edate') -and (Received -ge '$edate'} to limit your search result.

we can keep on technically finding the issues.
Or we can solve it by create a new mailbox and see how it goes.

Just one of the options :)
OK,
Yes it could be anything and that's what I'm trying to find out. I know I'm not the only one having this problem from what I'm seeing on the internet.

Checked health this morning and it looks good.
Indexing??????? hmmmm I'll check that out and re-index if possible (offline defrag?).
Search Query needing both a start date and end date, hmmmmmm I'll try that.
Create new mailbox. Possible. I can try that to eliminate that part of the equation but it will be another month before I can see results. This is because of the extra backup I do and the procedures we have in place.

I'll let you know the results as they happen.
SOLUTION
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
There appears not to be a really good resolution. Dan's script is what I'm currently using but it fails to get all emails and the new cmdlet doesn't allow for deleting. So my only option is to open the mailbox in question with a cached Outlook setup and manually delete the emails. Painfully slow but it's the only way to do it at this time. Closing this question as unresolved and to cleanup my question pools.