Jump to content

Recommended Posts

Posted

PowerShell Script for office365:

 

#This script will take take a user-imputed subject to delete Messages from
#Every mailbox within the Organisation

#ARGS
[string]$decision = "n"

Write-Host "This script requires the `"Discovery Management`" Exchange Role-Group PLUS `"Mailbox Import Export`" Role assigned to your Exchange onlineAdmin Account `nPlease add it before proceeding:"
Write-Host "`n`nEnter Administration Credentials"

$LiveCred = Get-Credential

#Pass Creds to Outlook.com and generate PS Session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
#Import PS Session/Grab MSOL Cmdlets
Import-PSSession $Session

$Subject = Read-Host "Please Enter a Message Subject to be deleted from ALL mailboxes:"

$decision = Read-Host "Are you sure youi want to delete messages with the subject " $Subject " (Y/N)?"

if ($decision -eq "y")
   {
   Write-Host "Deleting" $Subject
   Get-Mailbox -ResultSize unlimited | Search-Mailbox -SearchQuery "subject:$Subject" -DeleteContent -Confirm
   }
else
   {
   Write-Host "Nothing deleted"
   }
Remove-PSSession $Session
CD ..

Write-Host "Connection to MSOL closed"

 

Using the Search-Mailbox cmd with the DeleteContent switch requires that the admin account be added to the Discovery Management and Mailbox Import Export group.

 

If this is the situation, please go to the EAC (Exchange admin center) > permissions > admin roles to add your account into the corresponding groups to see if there is any improvement.

 

Warning: Use the above script with caution; When using the DeleteContent parameter, messages are permanently deleted from the user’s mailbox and cannot be recovered. (It could also take some time to run over a big Office365 Tenant)

 

For a full list of -SearchQuery parameters go here https://technet.microsoft.com/en-us/library/jj983804.aspx

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...