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:"email@example.com" -TargetMailbox info -TargetFolder SearchLogs -LogOnly -LogLevel Full
In that folder you find the logging of the files specified :
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:"firstname.lastname@example.org" –DeleteContent
Now all the files from the specified user are deleted. You can also use the -subject:’subjectname” switch.
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