Tuesday, May 21, 2019

Get a list of all domains where you have received emails on the last 60 days

The problem

How can I get a list of all domains that I've received email from in the last 2 months

 

 

The Solution

Over a PowerShell script to

a. Open a Powershell for exchange or simple powershell with administrative privilegies (using Run as Administrator):

 

It can be done using this following commands

The correct way to run the script will be as follows
#Change Executionpolicy (just in case you haven't done this already),
Set-ExecutionPolicy unrestricted
#Accept the warning (by pressing Y or A then press enter)

#Run the script (move to the folder where the script is downloaded) 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
$localdomain= "mydomain.com" # local domain public to be excluded on the search
Get-MessageTrackingLog -Server Mail -Start $( (Get-Date).ADddays(-60) ) -End $( Get-Date) -ResultSize Unlimited | select Recipients,EventId | where { ($_.EventId -eq "RECEIVE") -and $_.Recipients -notmatch "HealthMailbox*"}  | select -ExpandProperty Recipients | where{ $_ -notmatch $localdomain} | %{ $_.Split('@')[1]} | select -Unique
You will get the list of all unique domains list on the screen, if you want to have them in another format you can pipe
#Run the script (move to the folder where the script is downloaded) 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
$localdomain= "mydomain.com" # local domain public to be excluded on the search
Get-MessageTrackingLog -Server Mail -Start $( (Get-Date).ADddays(-60) ) -End $( Get-Date) -ResultSize Unlimited | select Recipients,EventId | where { ($_.EventId -eq "RECEIVE") -and $_.Recipients -notmatch "HealthMailbox*"}  | select -ExpandProperty Recipients | where{ $_ -notmatch $localdomain} | %{ $_.Split('@')[1]} | select -Unique | Out-File domains.txt
And that's it! thank you for reading

Please consider to donate and thank you very much for reading this.

No comments:

Post a Comment