Forum Discussion

Bukolaadefusi's avatar
Bukolaadefusi
Copper Contributor
Sep 14, 2023

azure(hybrid environment) Password Expiry Notification

I have a tenant where password expiry policy is set to notify users 14 days before expiry. 

My understanding is that they receive a pop-up 14 days before expiry with a link to change the password. 

 

Is it possible to receive an email notification by using PowerShell script to get the password expiry email notification

1 Reply

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi Bukolaadefusi,

    you can try to use a PowerShell script that queries Azure AD for user accounts with passwords that will expire within the next 14 days. Also it would be qood to use Task Scheduling for the script to run for example on a daily basis (How to run PowerShell scripts from Task Scheduler (spiceworks.com)).

    You can try this simple PowerShell script

     

     

     

    # Install the Azure AD module
    Install-Module AzureAD
    
    # Import the Azure AD module
    Import-Module AzureAD
    
    # Connect to Azure AD (you will be prompted for credentials, log in with your Global Admin account)
    Connect-AzureAD
    
    # Get users with passwords expiring the next 14 days
    $expiryDate = (Get-Date).AddDays(14)
    $usersToNotify = Get-MsolUser -All | Where-Object { $_.PasswordNeverExpires -eq $false -and $_.PasswordLastSet -ne $null -and $_.PasswordLastSet.AddDays(90) -lt $expiryDate }
    
    # Settings for sending email notification
    $smtpServer = "your-smtp-server"
    $smtpPort = 25
    $smtpUsername = "your-smtp-username"
    $smtpPassword = "your-smtp-password"
    $senderEmail = "your-sender-email"
    
    # Loop through all users and send notifications
    foreach ($user in $usersToNotify) {
        $toEmail = $user.UserPrincipalName
        $subject = "Password Expiry Notification"
        $body = "Your password is set to expire in 14 days. Please change it as soon as possible."
    
        Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl -From $senderEmail -To $toEmail -Subject $subject -Body $body -Credential (New-Object PSCredential $smtpUsername, (ConvertTo-SecureString $smtpPassword -AsPlainText -Force))
    }
    
    # Disconnect from Azure AD
    Disconnect-AzureAD

     

     

     


    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic

Resources