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