1

I have a script that I would like to dynamically send an email containing some info and go to certain people based on who in the company is running the script thanks to ActiveDirectory.

I have another portion of code that detects which OU the user or service account is running this from so for the sake of this question don't worry about that.

What I have so far is something like this:

function SendEmail 
{ 
Send-MailMessage -To $Recipients -From "Info <[email protected]>" -Subject "Account Report" -Body $Body -SmtpServer smtpserver.domain.com
}

 $To = Get-ADUser -Filter "(Name -like '*')" -Properties Name, EmailAddress -Searchbase $OUDetected | Format-List EmailAddress | Out-String

I am trying to utilize the Send-MailMessage cmdlet but emails have to be sent in a particular format to recipients.

A single recipient is:

"John Doe <[email protected]>"

Multiple recipients should look like:

"John Doe<[email protected]>", "Jane Doe <[email protected]>"

The issue is tricky then as the result of $To is in the format:

EmailAddress: [email protected]

EmailAddress: [email protected]

This format needs to be converted to the multiple recipient example as shown above.

I am at a loss at how to properly grab the data, format it and be sure it stays in the format that Send-Mailmessage can use.

TL;DR - There needs to be a way that grabs the email address(es) of each user(s) in a given OU in ActiveDirectory and format that/those email address(es) so it may send using Send-MailMessage

1 Answer 1

1

Send E-Mail from Powershell Script to Multiple Recipients based on ActiveDirectory User

This format needs to be converted to the multiple recipient example as shown above.

I am at a loss at how to properly grab the data, format it and be sure it stays in the format that Send-Mailmessage can use.

There needs to be a way that grabs the email address(es) of each user(s) in a given OU in ActiveDirectory and format that/those email address(es) so it may send using Send-MailMessage

Below is the PowerShell logic to get the output in the comma separated values excluding NULL values for the email address properties of those accounts your logic grabs.

I tested and confirmed this format works as expected in my environment without using the logic you state to not worry about (e.g. $OUDetected).

  • Please note the format Send-MailMessage with the -To parameter works just fine with a format of [email protected],[email protected] so each email address with only the local mailbox part and the domain name separated by commas is sufficient.

I'll put My Working Example below and I'll put the Your Example below based on what you gave and how I was able to confirm it worked fine in my case.

Your Example (with my working logic added)

function SendEmail 
{ 
Send-MailMessage -To $Recipients -From "Info <[email protected]>" -Subject "Account Report" -Body $Body -SmtpServer smtpserver.domain.com
}

 $To = (Get-ADUser -Filter "(Name -like '*')" -Properties Name, EmailAddress -Searchbase $OUDetected | Where-Object {$_.EmailAddress -ne $null} | Select -ExpandProperty EmailAddress) -join "," | Out-String

Your Example (Iterated To Addresses)

function SendEmail 
{
Send-MailMessage -To $To -From "Info <[email protected]>" -Subject "Account Report" -Body $Body -SmtpServer smtpserver.domain.com
}

$ToAddresses = Get-ADUser -Filter "(Name -like '*')" -Properties Name, EmailAddress -Searchbase $OUDetected | 
Where-Object {$_.EmailAddress -ne $null} | Select -ExpandProperty EmailAddress

ForEach ($ToAddress in $ToAddresses) {
   $To = $ToAddress
   SendEmail
}

My Working Example

(Get-ADUser -Filter "(Name -like '*')" -Properties Name, EmailAddress | Where-Object {$_.EmailAddress -ne $null} | Select -ExpandProperty EmailAddress) -join "," | Out-String
1
  • This iteration did it! I was able to send it to multiple recipients. Unfortunately they send as individual emails but this is much better than not sending at all. Thank you for your repeated assistance! Edit: Actually changing $ToAddress to $To and not using the foreach loop did let me send to all recipients. Thanks again! Jan 27, 2016 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .