Required Email Alert to administrators if any O365 Email account has not logged-in since 90 days

Dinesh Kumar 0 Reputation points
2023-08-11T06:34:51.7566667+00:00

Dear Friends,

We have O365 exchange online, we need the following feature with this.

 

  1. Email notification alerts if any end user didn't log in to Office 365 in the last 90 days.
  2. Email id should be blocked/disabled if a user didn't change his password in the last 90 days or didn't logged-in since 90 days

 

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,198 questions
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
{count} votes

2 answers

Sort by: Most helpful
  1. Brian Zarb 1,685 Reputation points
    2023-08-11T06:46:41.1233333+00:00

    Hi Dinesh, the easiest way to do this is using PowerShell with the Azure module.

    firstly, install the module (if you don't have it) and connect with Azure.

    If you're looking to automate the below, you only need the Connect-AzureAd and pass the credentials in a param.

    Install-Module AzureAD
    Connect-AzureAD
    

    Run the following script to check for inactive users.

    $daysInactive = 90
    $dateCutoff = (Get-Date).AddDays(-$daysInactive)
    $inactiveUsers = Get-AzureADUser -All $true | Where-Object { $_.LastDirSyncTime -lt $dateCutoff }
    $inactiveUsersEmails = $inactiveUsers | Select-Object -ExpandProperty UserPrincipalName
    

    For the users who haven't changed their password or logged in in the last 90 days

    foreach ($user in $inactiveUsers) {
        $passwordPolicies = Get-AzureADUserPasswordPolicies -ObjectId $user.ObjectId
    
        if ($passwordPolicies.LastPasswordChangeTimestamp -lt $dateCutoff) {
            # Disable the account
            Set-AzureADUser -ObjectId $user.ObjectId -AccountEnabled $false
    
            # You can also send an email notification here if desired
        }
    }
    

    Use Send-MailMessage cmdlet in PowerShell:

    $smtpServer = "your.smtp.server.com"
    $from = "******@yourdomain.com"
    $to = "******@yourdomain.com"
    $subject = "Inactive O365 Users Alert"
    $body = "The following users have not logged in for the past 90 days: `r`n" + ($inactiveUsersEmails -join "`r`n")
    Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -Body $body
    
    

    If you want to automate this, you can use some type of workflow automation such as the Task scheduler, and run it once a day.

    I hope this helps, if it does please mark it as the answer

    0 comments No comments

  2. Siva C 0 Reputation points
    2024-09-11T16:27:16.09+00:00

    Is there any chance to set an alert for users who haven’t logged in for more than 15 days?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.