Locate Inactive Active Directory Users

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s