A nice simple script to create automatic email notifications of inactive users within your Active Directory environment:
# Please Configure the following variables.... $smtpServer="10.10.10.10" $domain = "Domain.Name" $from = "IT Alert <notifications@company.com>" $recipient = "Your.Email@inhere.com" $company = "Your Companies Name" #If youre an MSP this makes it easier to filter incoming emails from separate domains $DaysInactive = 35 #Amount of days inactive you wish to alarm for $time = (Get-Date).Adddays(-($DaysInactive)) ######################################################################## # System Settings $textEncoding = [System.Text.Encoding]::UTF8 $date = Get-Date -format ddMMyyyy # End System Settings # Get all AD User with lastLogonTimestamp less than our time and set to enable # Output Name and lastLogonTimestamp into $data var $data = Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp | select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} $names = $data.Name # Email Subject Set Here $subject="$company Inactive User Report" # Email Body Set Here, Note You can use HTML, including Images. $body ="<p>The following <b>$company</b> user accounts have been inactive for more than $aysInactive days. Please confirm their status and disable if required.<br> <p>" foreach($name in $names){ $body = "$body<br>$name" } $body = "</p>$body<br> </P>" # Send Email Message Send-Mailmessage -smtpServer $smtpServer -from $from -to $recipient -subject $subject -body $body -bodyasHTML -priority High -Encoding $textEncoding # End