Script – Exchange 2007 – Delete mailbox items

Write-host “
#################Supporting Environment – Exchange 2007#####################
Script to remove items from a folder in user’s mailbox Multiple mailboxes
Adds full access mailbox permission to all the mailboxes
Moves all the items to a the target mailbox inside inbox\target folder
Once when the items are successfully removed, we can delete the target mailbox
Transcripts are saved in C:\trans.txt
############################################################################”

Start-transcript C:\trans.txt
$u = Read-host “

Enter the Account


write-host “

Enter the database name in this format Servername\Storage Group\Databasename
————————————–

$db = Read-host “Enter the Database name”
$s = Read-host “Server Name”
Write-host “
—————————————————————————
##Note: Please enter the folder name in “\foldername” format eg., “\outbox”
—————————————————————————

$F = Read-host “Folder from which items need to be deleted”
$tm = read-host “Enter the Target mailbox”

#Write-host “Do you wish to continue adding permission on all the mailboxes” -confirm:$true
#get-mailbox -server $s | % {add-mailboxpermission -user $U -accessrights Fullaccess -Identity $_}
Get-MailboxDatabase $db | Get-Mailbox | % {add-mailboxpermission -user $U -accessrights Fullaccess -Identity $_}

Write-host “Process Complete – Permission Granted”

#get-mailbox -server $s | % {Export-Mailbox -identity $_ -IncludeFolders “\outbox” -TargetMailbox $tm -TargetFolder “inbox\target” -DeleteContent -confirm:$false -Verbose}

Get-MailboxDatabase $db | Get-Mailbox | % {Export-Mailbox -identity $_ -IncludeFolders $F -TargetMailbox $tm -TargetFolder “inbox\target” -DeleteContent -confirm:$false -Verbose}

Write-Host “

**********End of Script*************”

Stop-transcript

=====================================================

Created the above script for deleting emails from a folder in multiple mailboxes (all at a time) – Database specific

This will be very useful when working on a log growth issue where in we need to check and remove items in user’s outbox folder. Its a pain to delete them one by one, Guess this will be useful.

Create a empty mailbox (eg., a mailbox named “target” and make sure the account through which the script is ran has full mailbox access

Before running this script, know what this is gonna do,

Script to remove items from a folder in user’s mailbox Multiple mailboxes

  •  Adds full mailbox permission to all the mailboxes to the user account which is entered.
    Moves all the items to a the target mailbox inside inbox\target folder
    Once when the items are successfully removed, we can delete the target mailbox
    Transcripts are saved in C:\trans.txt

Procedure:

  1.        Open EMS > go the path where you have placed the script > then type .\ Script_Removemailboxitems.ps1
  2.        Now the script is initiated and the transcript is by default saved in C:\trans.txt
  3.        Now the process starts,
  4.        It first adds full access permission on all the mailboxes in the provided mailbox database(database name entered)       for the account entered
  5.        Then it starts checking for the items in the appropriate folder which is provided as the target folder
  6.        It exports those items to  the target mailbox and deleted them from the source.
  7.        It’s done for all the mailboxes in the database.
  8.        It summarizes the changes made on all the mailboxes in the database and those are recorded in the C:\trans.txt
  9.       When you login to the target mailbox, it has all the items which was deleted from the source mailboxes ,

Run this on a test environment first and then put it on production.

Note: Also there are few line in the script which are hashed (#), those are for deleting mailbox items specific to a server

=

====================================================Technet post