Requirements:Active Directory Module for Windows Powershell
Code:
Search-ADAccount -LockedOut | Where {$_.UserPrincipalName -like "*microsoft.com*" -and $_.LastLogonDate -gt (Get-Date).AddDays(-30)} | Where {$_.Enabled -eq "True"} | Export-Csv -Path C:\Users\$env:USERNAME\desktop\locked.csv
note: change microsoft.com to your company domain
This line of code will search your active directory accounts for all Locked users where their email domain name matches the listed email domain, the user's last logon date is within the last 30 days and where the account is enabled, then it export the results to the current user's desktop as locked.csv
Search-ADAccount -LockedOut | Where {$_.UserPrincipalName -like "*microsoft.com*" -and $_.LastLogonDate -gt (Get-Date).AddDays(-30)} | Where {$_.Enabled -eq "True"} | Unlock-ADAccount
Same thing as the first line of code except instead of exporting the results, it unlocks those accounts shown in the results.