Arne
Arne

Reputation: 495

Setting up Outlok Email to AD (Active Directory) Group Members in Power Shell

I am working on a PowerShell script to do the following: pull all the email addresses from an existing Active Directory (AD) group and then populate a new, open Outlook email with those addresses for notification of group members.

Here are the pieces I have got in place and both work"

1) Grab the email addresses:

Get-ADGroupMember -identity "My Group" | Where-Object {$_.objectClass -eq 'user'} | Get-ADUser 
-Properties EmailAddress | where {$_.EmailAddress -ne $null} | select EmailAddress 

2) Start a new email in Outlook, ready to send (Found this snippet on the web)

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "<subject>"
$mail.Body = "<body>"
$mail.save()

$inspector = $mail.GetInspector
$inspector.Display()

So the piece I am missing is how to pipe the output from 1) into the email.Recipients field of the new email? Who can help me with this?

Upvotes: 0

Views: 170

Answers (2)

Trey Nuckolls
Trey Nuckolls

Reputation: 591

If you are looking to send an item to each address returned by the first part then something like this should work.

$Group = Get-ADGroup "My Group"
$Members = Get-ADGroupMember $Group | Where-Object {$_.objectClass -eq 'user'}
ForEach($Member in $Members){
$CurrentDN = $Member.distinguishedName
$CurrentMember = Get-ADUser $CurrentDN -Properties mail| where {$_.mail -ne $null}
$EmailTo=$EmailTo + $CurrentMember.mail + "; "}
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.To = $EmailTo
$mail.Subject = "<subject>"
$mail.Body = "<body>"
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

Set the MailItem.To/CC/BCC properties to a ";" separated list of addresses. Or call MailItem.Recipients.Add for each address.

Upvotes: 0

Related Questions