Import-Module ActiveDirectory
# Define the recipient email address
$recipient = "you@yourdomain.com"
# Define the subject of the email
$subject = "Active Directory User Report"
# Define the body of the email
$body = "Attached is a report of all Active Directory users."
# Get all Active Directory users in the OUName OU and select specific properties
$users = Get-ADUser -Filter * -SearchBase "OU=OUName,DC=DCName,DC=local" -Properties DisplayName, EmailAddress, LastLogonTimeStamp, Enabled, PwdLastSet | Select-Object DisplayName, EmailAddress, @{N='Last Logon'; E={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}}, @{Name="Enable/Disable"; Expression={if ($_.Enabled -eq $true) { "Enabled" } else { "Disabled" }}}, @{Name='Pwd Last Changed';Expression={[DateTime]::FromFileTime($_.PwdLastSet)}}
# Export the user information to a CSV file
$filePath = "C:\ActiveDirectoryUsers.csv"
$users | Export-Csv $filePath -NoTypeInformation
# Define the smtp2go account credentials (Free smtp service)
$username = "smtp2go_username"
$password = "smtp2go_password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
# Send the email with the CSV file as an attachment using SMTP2Go
Send-MailMessage -From "you@yourdomain.com" -To $recipient -Subject $subject -Body $body -Attachments $filePath -SmtpServer "mail.smtp2go.com" -Port 587 -UseSsl -Credential $cred