Skip to content

Instantly share code, notes, and snippets.

@azure365pro
Created May 13, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azure365pro/0778145fe7cd086dc34289a6b335c02d to your computer and use it in GitHub Desktop.
Save azure365pro/0778145fe7cd086dc34289a6b335c02d to your computer and use it in GitHub Desktop.
Discovery Search Simplified Script to Delete Specific Mails from Org
#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"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment