Powershell: Email last login for all active Active Directory Users

powershellWe use this to keep track of domain users. We watch for logins that are getting old, and disable the account. This will email a list of all active (non-disabled) users last login date/time, to the email address defined. You will need an email to send this from also.

Time needed: 15 minutes.

Configure the following details

  1. Variables

    $EmailUser = “email@email.com
    $EmailPass = ‘password
    $EmailFrom = “email@email.com
    $EmailTo = “email@email.com
    $SMTPServer = “smtp.gmail.com
    $SMTPPort = ###

  2. Configure a Scheduled task to run this script weekly

    I like it to show up Monday morning.

Files = Get-ADComputer -Filter 'OperatingSystem -like "*"' | Select-Object Name | Sort-Object Name | ForEach-Object { if (Test-Connection -ComputerName $_.Name -count 1 -quiet){(Get-HotFix -ComputerName $_.Name | sort installedon)[-1]} else { Write-Output $_.Name "is offline."}}
$Body = Format-Table -InputObject $Files    | Out-String
$Subject = "Active Users Last Logins"
Write-Host $Body

$EmailUser = "email@email.com"
$EmailPass = 'password'
$EmailFrom = "email@email.com"
$EmailTo = "email@email.com"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = ###
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailUser, $EmailPass); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Add a Comment

Your email address will not be published. Required fields are marked *