How to clear Exchange mail queues using PowerShell for a specific user

Recently, my organization were victim of a spam attack. Sadly, some users provided their email and password. The attacker set up a long queue of emails to be sent (from the victims account).

We had to delete all the message manually (from the queue viewer). It was very painful (more than 100 thousands emails...).

Is it possible to clear the email queue of a specific user with the PowerShell ? (using the Active Directory module)

Found this old post, but it look outdated and it delete all the queue...


Solution 1:

You just have to add a Where-Object (abbreviated to ?) after Get-Message to select the messages you want to remove.

Get-ExchangeServer |
    ?{$_.IsHubTransportServer -eq $true} |
    Get-Queue |
    get-message |
    ? {$_.sender -eq '[email protected]'} |
    Remove-Message -withNDR $false