Exchange 2007 & 2010: Delete all emails from a mailbox without deleting "Contacts"

Considering you have lets say few MBs in a txt file located on your c drive...

The script will read the MBs from the txt and without asking for confirmation (you can change to true) will perform the deletion of ONLY emails without touching your Contacts.

Exchnage 2007
Get-Content C:\mailboxes.txt| Export-Mailbox -ExcludeFolders "\Contacts" -DeleteContent -confirm:$false

Exchnage 2010
(Please check for Prerequisites on Import-Export Role to be enabled and required security groups)

Get-Content C:\mailboxes.txt| Search-Mailbox -SearchQuery "kind:email" -DeleteContent -force

* NOTE: You can add a schedule job or task to automatically do this on weekly bss or as you need!

* NOTE2: The way to stop prompting is to use the -Force parameter (if the cmdlet supports it).
Setting -Confirm to $false means "prompt me if needed".
Setting -Confirm to $true means "prompt me every time"

You need a txt file with mailboxes, a yourfilename.ps1 file with the command and a .bat file to call the ps1 file & powershell

bat ex. for 2007:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\mailboxes.ps1'"

bat ex. for 2010: 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\V14\bin\exshell.psc1" -command ". 'C:\mailboxes.ps1'"

Comments