I would like to email a list of AD users as a CSV but I don’t want to have to save the CSV to disk before I email it.
Here is the code that gets me the data:
get-aduser -filter * -properties Telephonenumber |
where Telephonenumber -ne $Null |
select givenname, surname, telephonenumber |
sort surname
Now I want to add on something like:
| Export-Csv |
Send-MailMessage -From HelpDesk@company.com -to someone@company.com -subject "Outlook address book export" -SmtpServer EdgeTransport
Is there anyway to add the CSV data as an attachment in memory without saving the file to the file system?
The
-Attachmentsparameter forSend-MailMessagerequires an array of paths, so you must write a file to disk somewhere.To send your CSV data as the body of the email, use
convertto-csv -notypeinformationinstead ofexport-csv.