{"id":135,"date":"2023-02-13T20:08:31","date_gmt":"2023-02-13T20:08:31","guid":{"rendered":"https:\/\/jasonisenhart.com\/?p=135"},"modified":"2023-02-13T21:24:41","modified_gmt":"2023-02-13T21:24:41","slug":"powershell-ad-user-csv-to-email","status":"publish","type":"post","link":"https:\/\/jasonisenhart.com\/index.php\/2023\/02\/13\/powershell-ad-user-csv-to-email\/","title":{"rendered":"Powershell AD User CSV to Email"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>Import-Module ActiveDirectory\n\n# Define the recipient email address\n$recipient = \"you@yourdomain.com\"\n\n# Define the subject of the email\n$subject = \"Active Directory User Report\"\n\n# Define the body of the email\n$body = \"Attached is a report of all Active Directory users.\"\n\n# Get all Active Directory users in the OUName OU and select specific properties\n$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={&#91;DateTime]::FromFileTime($_.LastLogonTimeStamp)}}, @{Name=\"Enable\/Disable\"; Expression={if ($_.Enabled -eq $true) { \"Enabled\" } else { \"Disabled\" }}}, @{Name='Pwd Last Changed';Expression={&#91;DateTime]::FromFileTime($_.PwdLastSet)}}\n\n# Export the user information to a CSV file\n$filePath = \"C:\\ActiveDirectoryUsers.csv\"\n$users | Export-Csv $filePath -NoTypeInformation\n\n# Define the smtp2go account credentials (Free smtp service)\n$username = \"smtp2go_username\"\n$password = \"smtp2go_password\"\n$secstr = New-Object -TypeName System.Security.SecureString\n$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}\n$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr\n\n# Send the email with the CSV file as an attachment using SMTP2Go\nSend-MailMessage -From \"you@yourdomain.com\" -To $recipient -Subject $subject -Body $body -Attachments $filePath -SmtpServer \"mail.smtp2go.com\" -Port 587 -UseSsl -Credential $cred<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"108\" src=\"https:\/\/jasonisenhart.com\/wp-content\/uploads\/2023\/02\/image-1024x108.png\" alt=\"\" class=\"wp-image-136\" srcset=\"https:\/\/jasonisenhart.com\/wp-content\/uploads\/2023\/02\/image-1024x108.png 1024w, https:\/\/jasonisenhart.com\/wp-content\/uploads\/2023\/02\/image-300x32.png 300w, https:\/\/jasonisenhart.com\/wp-content\/uploads\/2023\/02\/image-768x81.png 768w, https:\/\/jasonisenhart.com\/wp-content\/uploads\/2023\/02\/image.png 1165w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">This is what the output will look like after adding a table in excel.<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,16,13,14,17],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-it-how-to","tag-active-directory","tag-ad-user-info","tag-csv-to-email","tag-powershell","tag-powershell-reports"],"_links":{"self":[{"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":2,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonisenhart.com\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}