37.8 C
Dubai
Tuesday, April 23, 2024

Discovery Search Simplified Script to Delete a Specific Email and other Tasks – Exchange 2010 & 2013

Updated – Script Supports Exchange 2013 and Exchange 2016 and Office 365

Download the Script

You can Use this Script to for the Below Tasks.

I would Strongly Recommended you to understand this script & Run it on your lab – Confirm it suits your Need

As it has the Ability to Delete Content in some tasks, Please be safe using it

Before running on the Production Server, Please Test it

Open Exchange Management Shell , Browse to the Location of the Script


PS C:\> .\DiscoverySearchSimplified.ps1

Discovery Search Simplified
----------------------------

1.Check for Discovery Search Permissions

2.Check for Mailbox Import Export Permissions

3. Add a User to Discovery Search Permission

4. Add a User to Mailbox Import Export Permission
Make Sure Discovery Search & Mailbox Import Export Permissions are Added to the Current Logged on User
To use Options greater than 4
5. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email

6. Remove a Email with a Specific Subject from One Mailbox

7. Remove a Email with a Specific Subject from all the Mailboxes in the Organization

8. Mailbox Cleanup - Emptying a Specific Mailbox (Exch2010 only)

9. Mailbox Dumpster Cleanup - Emptying a Specific Mailbox Dumpster(Exch2010 only)

10. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es From Address

11. Remove a Email with a Specific Subject from One Mailbox and Requires From Address

12. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From Address

13. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es To Address

14. Remove a Email with a Specific Subject from One Mailbox and Requires To Address

15. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires To Address

16. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es From&To Address

17. Remove a Email with a Specific Subject from One Mailbox and Requires From&To Address

18. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From&To Address

Choose The Task: 6
Enter Alias of the User to Search for a Subject and Delete it: galsync1
Enter the Subject: Spam Email !!

Confirm
Deleting content from mailboxes galsync1
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Identity         : careexchange.in/Users/Galsync1
TargetMailbox    :
TargetPSTFile    :
Success          : True
TargetFolder     :
ResultItemsCount : 3
ResultItemsSize  : 29.82 KB (30,535 bytes)    

In the Above Example – I removed a Spam Email with a Subject “Spam Email !!”

Without disturbing the User

sample

Example –

With From to Options –  withFromTo

Hope it will help Exchange Admins to simplify their work.

Download the Script


#Requires -version 2
<#
.SYNOPSIS
DiscoverySearchSimplified.ps1 - It Can Check for Required Permissions to do a Discovery Search

1.Check for Discovery Search Permissions

It will ask for the User Alias, Checks the User is a member of "Discovery Management" Role Group

2.Check for Mailbox Import Export Permissions

It will ask for the User Alias , Checks the User is a member of "Mailbox Import Export" Management Role

3. Add a User to Discovery Search Permission

It will ask for the User Alias, Adds the user a member of "Discovery Management" Role Group

4. Add a User to Mailbox Import Export Permission"

It will ask for the User Alias, Adds the user a member of "Mailbox Import Export" Management Role

5. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email

It will ask for the User Alias , Asks for the Subject , Asks for the Mailbox name where it can send a report

6. Remove a Email with a Specific Subject from One Mailbox

It will ask for the User Alias , Asks for the Subject , Removes it

7. Remove a Email with a Specific Subject from all the Mailboxes in the Organization

It will ask for the User Alias , Asks for the Subject , Removes it from the Entire Organization asking for Confirmation

It won't run for all the Mailboxes Unless you choose - [A] Yes to All

8. Mailbox Cleanup - Emptying a Specific Mailbox

It will ask for the User Alias , Empties the Entire Mailbox

9. Mailbox Dumpster Cleanup - Emptying a Specific Mailbox Dumpster

It will ask for the User Alias , Empties the Dumpster Alone

.DESCRIPTION

Simplifies the Usage for Discovery Search Feature

.OUTPUTS
Results are output to the PowerShell window.

.EXAMPLE

[PS] C:\Scripts>.\DiscoverySearchSimplified.ps1
Discovery Search Simplified
----------------------------

1.Check for Discovery Search Permissions

2.Check for Mailbox Import Export Permissions

3. Add a User to Discovery Search Permission

4. Add a User to Mailbox Import Export Permission
Make Sure Discovery Search & Mailbox Import Export Permissions are Added to the Current Logged on User
To use Options greater than 4
5. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email

6. Remove a Email with a Specific Subject from One Mailbox

7. Remove a Email with a Specific Subject from all the Mailboxes in the Organization

8. Mailbox Cleanup - Emptying a Specific Mailbox (Exch2010 only)

9. Mailbox Dumpster Cleanup - Emptying a Specific Mailbox Dumpster(Exch2010 only)

10. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es From Address

11. Remove a Email with a Specific Subject from One Mailbox and Requires From Address

12. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From Address

13. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es To Address

14. Remove a Email with a Specific Subject from One Mailbox and Requires To Address

15. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires To Address

16. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requir
es From&To Address

17. Remove a Email with a Specific Subject from One Mailbox and Requires From&To Address

18. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From&To Address -Fore
Ground Cyan
Note :

- Adding Permissions Doesn't Reflect on the Powershell Instantly, You got to Close and Reopen Powershell to Refresh the
Permissions
Choose The Task: 6
Enter Alias of the User to Search for a Subject and Delete it: galsync1
Enter the Subject: trrddd

Confirm
Deleting content from mailboxes galsync1
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
Identity : careexchange.in/Users/Galsync1
TargetMailbox :
TargetPSTFile :
Success : True
TargetFolder :
ResultItemsCount : 3
ResultItemsSize : 29.82 KB (30,535 bytes)

.NOTES
Written By: Satheshwaran Manoharan
Website : Www.careexchange.in

Change Log
V1.0, 20/09/2012 - Initial version

Change Log
V1.1, 15/08/2016 - Add Support to Exchange 2016 with From-To Options to Search and delete
#>

#Add Exchange 2010 snapin if not already loaded
if (!(Get-PSSnapin | where {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"}))
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
}

Write-host "

Discovery Search Simplified
----------------------------

1.Check for Discovery Search Permissions

2.Check for Mailbox Import Export Permissions

3. Add a User to Discovery Search Permission

4. Add a User to Mailbox Import Export Permission" -ForeGround "Cyan"
Write-host "

Make Sure Discovery Search & Mailbox Import Export Permissions are Added to the Current Logged on User
To use Options greater than 4" -ForeGround "Green"

Write-host "

5. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email

6. Remove a Email with a Specific Subject from One Mailbox

7. Remove a Email with a Specific Subject from all the Mailboxes in the Organization

8. Mailbox Cleanup - Emptying a Specific Mailbox (Exch2010 only)

9. Mailbox Dumpster Cleanup - Emptying a Specific Mailbox Dumpster(Exch2010 only)

10. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires From Address

11. Remove a Email with a Specific Subject from One Mailbox and Requires From Address

12. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From Address

13. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires To Address

14. Remove a Email with a Specific Subject from One Mailbox and Requires To Address

15. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires To Address

16. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires From&To Address

17. Remove a Email with a Specific Subject from One Mailbox and Requires From&To Address

18. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From&To Address -ForeGround "Cyan""

Write-host "

Note :

- Adding Permissions Doesn't Reflect on the Powershell Instantly, You got to Close and Reopen Powershell to Refresh the Permissions

" -ForeGround "Green"
$number = Read-Host "Choose The Task"

switch ($number)
{
#1.Check for Discovery Search Permissions
1 {

$GetAlias = Read-Host "Enter Alias of the User to Check for Discovery Search Permissions"

$Discovery = ((Get-RoleGroupMember "Discovery Management") | where-Object{$_.Name -eq "$GetAlias"}).Name -eq "$GetAlias"

if ($Discovery -eq 'True')

{

"$GetAlias is a Member of Discovery Management"

}

Else

{

Write-Host "$GetAlias is not a Member of Discovery Management" -foreground "RED"

};

break}
#2.Check for Mailbox Import Export Permissions
2 {

$GetAlias = Read-Host "Enter Alias of the User to Check for Mailbox Import Export Permissions"

$MailboxImportExport = (((Get-ManagementRoleAssignment -Role "Mailbox Import Export" -GetEffectiveUsers) | Where-Object{$_.EffectiveUserName -eq "$GetAlias"}).Count -ge "1")

if ($MailboxImportExport -eq "true")
{

"$GetAlias is a Member of Mailbox Import Export Permission"

}

else
{

Write-Host "$GetAlias is not a Member of Mailbox Import Export Permission" -foreground "RED"

};

break}
#3. Add a User to Discovery Search Permission
3 {

$GetAlias = Read-Host "Enter Alias of the User to add Discovery Management Permissions"

Add-RoleGroupMember "Discovery Management" -Member "$GetAlias"

; break}
#4. Add a User to Mailbox Import Export Permission"
4 {

$GetAlias = Read-Host "Enter Alias of the User to add Mailbox Import Export Permissions"

New-ManagementRoleAssignment -Role "Mailbox Import Export" -User "$GetAlias"

; break}
#5. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email
5 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject"

$GetSubject = Read-Host "Enter the Subject"

$GetMailboxtoSendLog = Read-Host "Enter the Mailbox Alias to send the Generated Log"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject'" -Logonly -Targetmailbox "$GetMailboxtoSendLog" -Targetfolder "Inbox"

; break}
#6. Remove a Email with a Specific Subject from One Mailbox

6 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject and Delete it"

$GetSubject = Read-Host "Enter the Subject"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject'" -DeleteContent

; break}
#7. Remove a Email with a Specific Subject from all the Mailboxes in the Organization
7 {

$GetSubject = Read-Host "Enter the Subject of the Email to Delete from the Entire Organization"

get-Mailbox | search-mailbox -searchquery "Subject:'$GetSubject'" -DeleteContent

; break}
#8. Removed - Mailbox Cleanup - Emptying a Specific Mailbox
8 {

$GetAlias = Read-Host "Enter the Mailbox Name to Cleanup - Empyting a Mailbox"

search-mailbox -identity $GetAlias -searchquery "Subject:'*'" -DeleteContent

; break}
#9. Removed - Mailbox Dumpster Cleanup - Emptying a Specific Mailbox Dumpster
9 {

$GetAlias = Read-Host "Enter the Mailbox Name to Cleanup the dumpster Alone"

search-mailbox -identity $GetAlias -searchquery "Subject:'*'" -SearchDumpsterOnly -DeleteContent

; break}

#10. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires From Address
10 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

$GetMailboxtoSendLog = Read-Host "Enter the Mailbox Alias to send the Generated Log"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND from:$FromAddress" -Logonly -Targetmailbox "$GetMailboxtoSendLog" -Targetfolder "Inbox"

; break}
#11. Remove a Email with a Specific Subject from One Mailbox and Requires From Address
11 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject and Delete it"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND from:$FromAddress" -DeleteContent

; break}
#12. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From Address
12 {

$GetSubject = Read-Host "Enter the Subject of the Email to Delete from the Entire Organization"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
get-Mailbox | search-mailbox -searchquery "Subject:'$GetSubject' -AND from:$FromAddress" -DeleteContent

; break}
#13. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires To Address
13 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

$GetMailboxtoSendLog = Read-Host "Enter the Mailbox Alias to send the Generated Log"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND to:$ToAddress" -Logonly -Targetmailbox "$GetMailboxtoSendLog" -Targetfolder "Inbox"

; break}
#14. Remove a Email with a Specific Subject from One Mailbox and Requires To Address
14 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject and Delete it"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND to:$ToAddress" -DeleteContent

; break}
#15. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires To Address
15 {

$GetSubject = Read-Host "Enter the Subject of the Email to Delete from the Entire Organization"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
get-Mailbox | search-mailbox -searchquery "Subject:'$GetSubject' -AND to:$ToAddress" -DeleteContent

; break}
#16. Generate a Log - To identitfy the Number of Emails Exist in a mailbox Specifying the Subject of the Email and Requires From & To Address
16 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

$GetMailboxtoSendLog = Read-Host "Enter the Mailbox Alias to send the Generated Log"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND from:$FromAddress -AND to:$ToAddress" -Logonly -Targetmailbox "$GetMailboxtoSendLog" -Targetfolder "Inbox"

; break}
#17. Remove a Email with a Specific Subject from One Mailbox and Requires From & To Address
17 {

$GetAlias = Read-Host "Enter Alias of the User to Search for a Subject and Delete it"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
$GetSubject = Read-Host "Enter the Subject"

search-mailbox -identity $GetAlias -searchquery "Subject:'$GetSubject' -AND from:$FromAddress -AND to:$ToAddress" -DeleteContent

; break}
#18. Remove a Email with a Specific Subject from all the Mailboxes in the Organization and Requires From & To Address
18 {

$GetSubject = Read-Host "Enter the Subject of the Email to Delete from the Entire Organization"
$FromAddress = Read-Host "Enter from address of the Email Format user@domain.com"
$ToAddress = Read-Host "Enter To address of the Email Format user@domain.com"
get-Mailbox | search-mailbox -searchquery "Subject:'$GetSubject' -AND from:$FromAddress -AND to:$ToAddress" -DeleteContent

; break}
Default {Write-Host "No matches found , Enter Options 1 to 18" -ForeGround "red"}
}

Download the Script

Satheshwaran Manoharan
Satheshwaran Manoharanhttps://www.azure365pro.com
Award-winning Technology Leader with a wealth of experience running large teams and diversified industry exposure in cloud computing. From shipping lines to rolling stocks.In-depth expertise in driving cloud adoption strategies and modernizing systems to cloud native. Specialized in Microsoft Cloud, DevOps, and Microsoft 365 Stack and conducted numerous successful projects worldwide. Also, Acting as a Technical Advisor for various start-ups.

Related Articles

15 COMMENTS

  1. I love the way you wrote this article. This is wonderful. I do hope you intend to write more of these types of articles. Thank you for this interesting content!

  2. Amazing things here. I’m very satisfied to peer your article. Thank you so much and I’m taking a look forward to touch you. Will you kindly drop me a mail?

  3. Let’s have a beer or somethingYou can either stay or leave.This pen doesn’t write well.Unfortunately you’ll have to pay the fine before you check those books out.He can hardly speak.This is by far the largest cake in the world.This is by far the largest cake in the world.I will never forget it.If only I could fly.The two brothers look very much alike.
    uggs for cheap

  4. The script was useful in identifying the mail boxes (option 7 in the menu) from which select mail needed to be deleted. Thanks for the same.

  5. I just tested this to remove an one email from one users mailbox.
    They have a number of emails that start with “Free Coupons – T….”, but only 1 that matches exactly as the suffix is unique.

    I specified the user, and then the subject:
    Free Coupons – T1222-1

    It removed every email *except* the one I requested. All emails it removed started off “Free Coupons – “. Only 1 email matched the specific subject in its entirety.

    Yikes!

  6. Hi Admin

    Thanks for sharing script discovery search , We use Exchange 2013 , I Already test the script on Exchange 2010 and It Worked very well
    Now , I’m Trying to test for 2013 but no action after running the scirp example tested options 5 and 6

    I have Organization management permissions , Mailbox Import Export Permissions and Discovery Search Permission

    Missing some config to run this script , it’s really useful especially for removing spam and suspicious emails

LEAVE A REPLY

Please enter your comment!
Please enter your name here

× How can I help you?