Powershell : How to identify (and delete) specific email messages from Office 365

I was looking for a way to identify messages in my Outlook Mailbox and easily delete them.

First connect to Office 365 with you (admin) credentials using the LiveCred command.

Set the Execution Policy and import the commandlets.

Using the next command creates an export of all emails from the specified user in the emailbox username and puts that export in the mailbox of user target mailbox and creates an folder called Searchlogs.

$LiveCred = Get-Credential

Set-ExecutionPolicy RemoteSigned (Make sure you open Powershell with Administrator Rights!)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
$Session Get-Mailbox username | Search-Mailbox -SearchQuery from:"user@emailbox.nl" -TargetMailbox info -TargetFolder SearchLogs -LogOnly -LogLevel Full

 

In that folder you find the logging of the files specified :

Results_powershell

 

Open the zip file containing the CSV export.

 

When you are ready to delete those files use the following command :

Search-Mailbox -Identity username -SearchQuery From:"user@mail.nl" –DeleteContent

Now all the files from the specified user are deleted. You can also use the -subject:’subjectname” switch.

 

1 Comment

  1. Be sure to disconnect the remote PowerShell session when you’re finished. If you close the Windows PowerShell window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you’ll need to wait for the sessions to expire. To disconnect the remote PowerShell session, run the following command : Remove-PSSession $Session

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.