Get Mail Sent Receive Outside Working Hours PowerShell

Get Mail Sent Receive Outside Working Hours PowerShell

Hello,

In France, we have a law named “Droit à la déconnexion“, this literally means “Right to disconnect”. Its purpose is to protect French IT worker to allow them to disconnect from work when their day is done. For example, an employer can’t blame an employee for not checking their mails at 11PM Saturday night.

Today, I’ll share with you a quick script to check if people are sending or receiving mail outside of standard working hours. The script is based on the tracking logs and accepts a few parameters:

  • UserList: List of send/receiver to search for
  • StartDate: Start date for the query, defaulted to 7 days ago
  • EndDate: End date for the query, defaulted to now
  • Credential, credential to use to connect to Exchange Online

Note: The larger the time frame is, the longer the script will run.

The working hours are defined in the script as 8 to 8 during week days only. If you feel they are too large and not large enough, you can edit them in the calculated property named OutsideOfWorkingHours (you need to edit it twice, one time for sent email, and another time for received email):

@{Label='OutsideOfWorkingHours';Expression={if(
($_.Received.DayOfWeek -eq [System.DayOfWeek]::Saturday) -or 
($_.Received.DayOfWeek -eq [System.DayOfWeek]::Sunday) -or 
($_.Received.TimeOfDay.Hours -lt 8) -or 
($_.Received.TimeOfDay.Hours -gt 18) 
){$true}else{$false}}}

The script take care of the Exchange Online connection, but you can easily edit it to connect to your local Exchange deployment.

Hereunder a example about how you can use it, and the output to expect:

.\Get-MailOutsideofWorkingHours.ps1 -UserList @('dumbo@itfordummies.net','junk@itfordummies.net') | Where-Object -FilterScript {$_.OutsideOfWorkingHours -eq $true} | Out-GridView -Title 'Mail Outside of Working Hours !'
Get Mail Sent Receive Outside Working Hours PowerShell - Script Output

Get Mail Sent Receive Outside Working Hours PowerShell – Script Output

And now you can see who doesn’t respect and who is solicited outside of standard working hours.

Obviously, this script won’t prevent mail delivery, it will just allow you to have a look at the current situation, and maybe, take action for later. If you want something more aggressive, you can read that great article (in French).

Get Mail Sent Receive Outside Working Hours PowerShell – Download

You can find the script in my GitHub repo.

One thought on “Get Mail Sent Receive Outside Working Hours PowerShell

  1. Hey 🙂 J’avais vu le dernier article aussi … et oui tout le monde cherche des solutions à mettre en place en france ..

    merci pour le tuto / script

Leave a Reply